I found this
SWEET piece of code that promise to simplify my Nested GridView I have a project that's making use of this strategy quite a bit (Nested GridView). It places the nested grid in a UserControl. My question is how to bind the GridView in the UserControl to
the IList(Of T) DataSource. I'm thinking that I should Raise the event from within the DataBound event but i'm not sure. Your help would be much appreciated.
Protected Sub gvJobs_RowDataBound(sender As Object, e As GridViewRowEventArgs)
This Is how it gets done when the GridView Is nested directly in the Gridview
Dim row As GridViewRow = e.Row
Dim gv As GridView = New GridView()
gv = CType(row.FindControl("GridView2"), GridView)
gv.DataSource = ChildDataSource((CType(e.Row.DataItem, DataRowView))("startDate").ToString(), sortStr)
gv.DataBind()
This Is a sketchy code I tried in but it did'nt work.
Dim gv As GridView = New GridView()
gv = CType(row.FindControl("EZPassesControl1").FindControl("gvEZPasses"), GridView)
gv.DataSource = ChildDataSource((CType(e.Row.DataItem, DataRowView))("startDate").ToString(), strSort)
gv.DataBind()
End Sub
Do you want to operate the GridView nested in userControl inside parent GridView control, for example to set its data source. If so, you can refer to the following example:
demo.aspx:
Hi, Ruffone
Do you want to operate the GridView nested in userControl inside parent GridView control, for example to set its data source. If so, you can refer to the following example:
demo.aspx
<%@ Register TagPrefix="uc" TagName="UserInfoBoxControl" Src="~/uControlDemo.ascx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="demo_Form" runat="server">
<asp:GridView runat="server" ID="demoGV" AutoGenerateColumns="false" OnRowDataBound="demoGV_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="userControl">
<ItemTemplate>
<div style="height: 150px; width: 300px;">
<uc:UserInfoBoxControl runat="server" ID="uControl"></uc:UserInfoBoxControl>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TXT">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("txt") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
demo.aspx.cs
demo.aspx.cs
public partial class Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String conStr = "Server=.;Database=TestDB;Trusted_Connection=True;";
using (SqlConnection conn = new SqlConnection(conStr))
{
DataTable dt = new DataTable();
String sql = "select id,txt from demoDB";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
sda.Fill(dt);
demoGV.DataSource = dt;
demoGV.DataBind();
}
}
}
protected void demoGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
String conStr = "Server=.;Database=TestDB;Trusted_Connection=True;";
using (SqlConnection conn = new SqlConnection(conStr))
{
DataTable dt = new DataTable();
String sql = "select id,txt from demoDB";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
sda.Fill(dt);
uControlDemo uControl = (uControlDemo)e.Row.FindControl("uControl");
if (uControl != null) {
GridView row = (GridView)uControl.FindControl("uc_GridView");
row.DataSource = dt;
row.DataBind();
}
}
}
}
Member
26 Points
155 Posts
Bind Nested UserControl with a GridView to a parent GridView in an event
May 14, 2020 01:55 PM|Ruffone|LINK
I found this SWEET piece of code that promise to simplify my Nested GridView I have a project that's making use of this strategy quite a bit (Nested GridView). It places the nested grid in a UserControl. My question is how to bind the GridView in the UserControl to the IList(Of T) DataSource. I'm thinking that I should Raise the event from within the DataBound event but i'm not sure. Your help would be much appreciated.
<asp:GridView ID="gvJobs" runat="server" DataKeyNames="Id,JobNumber,StartDate" OnRowDataBound="gvJobs_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Job ID" InsertVisible="false">
<ItemTemplate>
<asp:Label ID="lblJobID" Text='<%# Eval("Id") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<div id="div<%# Eval("id") %>">
<uc1:EZPassesControl runat="server" ID="EZPassesControl1" Data='<%# Container.DataItem %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Protected Sub gvJobs_RowDataBound(sender As Object, e As GridViewRowEventArgs)
This Is how it gets done when the GridView Is nested directly in the Gridview
Dim row As GridViewRow = e.Row
Dim gv As GridView = New GridView()
gv = CType(row.FindControl("GridView2"), GridView)
gv.DataSource = ChildDataSource((CType(e.Row.DataItem, DataRowView))("startDate").ToString(), sortStr)
gv.DataBind()
This Is a sketchy code I tried in but it did'nt work.
Dim gv As GridView = New GridView()
gv = CType(row.FindControl("EZPassesControl1").FindControl("gvEZPasses"), GridView)
gv.DataSource = ChildDataSource((CType(e.Row.DataItem, DataRowView))("startDate").ToString(), strSort)
gv.DataBind()
End Sub
Contributor
3140 Points
983 Posts
Re: Bind Nested UserControl with a GridView to a parent GridView in an event
May 15, 2020 09:01 AM|Yang Shen|LINK
Hi Ruffone,
Do you want to operate the GridView nested in userControl inside parent GridView control, for example to set its data source. If so, you can refer to the following example:
demo.aspx:
demo.aspx.cs
uControlDemo.ascx
Result:
If this is not the result you expect, please clearly state what you want to do and provide more information, mainly including:
Hope it can help you.
Best Regard,
Yang Shen