RSS

Nested GridView in C#.Net

01 Sep

Inside the gridview’s templatefield of itemtemplate you can add one more gridview.

In .aspx page,

<asp:GridView ID=”GridView1″ runat=”server”>
<Columns>
<asp:BoundField DataField=”SNo” HeaderText=”S.No”/>
<asp:BoundField DataField=”CategoryName” HeaderText=”Product Name”/>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID=”GridView2″ runat=”server” AutoGenerateColumns=”false”>
<Columns>
<asp:BoundField DataField=”SNo” HeaderText=”S.No”/>
<asp:BoundField DataField=”CategoryName” HeaderText=”Product Name”/>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

In code behind,

protected void Page_Load(object sender, EventArgs e)
{
da.Fill(dt);
GridView1.DataSource = dt; // Data source for outer grid
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl(“GridView2”); //Finding inner grid
DataTable dt = new DataTable();
da.Fill(dt);
gv.DataSource = dt; // Data source for inner grid.
gv.DataBind();
}
}

 
Leave a comment

Posted by on September 1, 2012 in C#.NET

 

Leave a comment