Bind Nested GridView in ASP.Net

This article shows how to bind a nested GridView and work with it. The following is the procedure.

Step 1

First we create two tables in a database as in the following:

  1. create table TeamLeader(  
  2.    id int identity(1,1) Primary key,  
  3.    Name nvarchar(30),  
  4.    Age int,  
  5.    Branch nvarchar(10)  
  6. )  
And:
  1. Create  table TeamMember(  
  2.    iid int identity(1,1) primary key,  
  3.    TLid int CONSTRAINT FKTeamMemberPKTeamMember REFERENCES TeamLeader(id),  
  4.    MemberName [nvarchar](50),  
  5.    Age int,  
  6.    Branch nvarchar(50)  
  7. )  
Step 2

Insert some data into the table like the following:

Insert some data into table

Table

Step 3

Now create a website, add a webpage and copy the design code as in the following:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Example.aspx.cs" Inherits="Example" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <asp:GridView ID="GVLeader" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="lightgreen"  
  12.             DataKeyNames="id" OnRowCommand="GVLeader_RowCommand">  
  13.             <Columns>  
  14.                 <asp:TemplateField HeaderText="Team Leader Name">  
  15.                     <ItemTemplate>  
  16.                         <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>  
  17.                     </ItemTemplate>  
  18.                 </asp:TemplateField>  
  19.                 <asp:TemplateField HeaderText="Age">  
  20.                     <ItemTemplate>  
  21.                         <asp:Label ID="Label2" runat="server" Text='<%# Eval("Age") %>'></asp:Label>  
  22.                     </ItemTemplate>  
  23.                 </asp:TemplateField>  
  24.                 <asp:TemplateField HeaderText="Branch">  
  25.                     <ItemTemplate>  
  26.                         <asp:Label ID="Label3" runat="server" Text='<%# Eval("Branch") %>'></asp:Label>  
  27.                     </ItemTemplate>  
  28.                 </asp:TemplateField>  
  29.                 <asp:TemplateField HeaderText="Team Member" ControlStyle-BackColor="#EBEBEB">  
  30.                     <ItemTemplate>  
  31.                         <asp:Button ID="Button1" runat="server" Text="See Member" CommandName='<%#Eval("id") %>' CommandArgument='<%#  
  32.             Container.DataItemIndex%>' />  
  33.                         <asp:GridView ID="GVMember" AutoGenerateColumns="false" runat="server" AutoGenerateEditButton="true"  
  34.                             DataKeyNames="iid" OnRowCancelingEdit="GVMember_RowCancelingEdit" OnRowEditing="GVMember_RowEditing"  
  35.                             OnRowUpdating="GVMember_RowUpdating">  
  36.                             <Columns>  
  37.                                 <asp:TemplateField HeaderText="Name">  
  38.                                     <ItemTemplate>  
  39.                                         <asp:Label ID="Label4" runat="server" Text='<%#Eval("MemberName") %>'></asp:Label>  
  40.                                     </ItemTemplate>  
  41.                                     <EditItemTemplate>  
  42.                                         <asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("MemberName") %>'></asp:TextBox>  
  43.                                     </EditItemTemplate>  
  44.                                 </asp:TemplateField>  
  45.                                 <asp:TemplateField HeaderText="Age">  
  46.                                     <ItemTemplate>  
  47.                                         <asp:Label ID="Label5" runat="server" Text='<%#Eval("Age") %>'></asp:Label>  
  48.                                     </ItemTemplate>  
  49.                                     <EditItemTemplate>  
  50.                                         <asp:TextBox ID="TextBox5" runat="server" Text='<%#Eval("Age") %>'></asp:TextBox>  
  51.                                     </EditItemTemplate>  
  52.                                 </asp:TemplateField>  
  53.                                 <asp:TemplateField HeaderText="Branch">  
  54.                                     <ItemTemplate>  
  55.                                         <asp:Label ID="Label6" runat="server" Text='<%#Eval("Branch") %>'></asp:Label>  
  56.                                     </ItemTemplate>  
  57.                                     <EditItemTemplate>  
  58.                                         <asp:TextBox ID="TextBox6" runat="server" Text='<%#Eval("Branch") %>'></asp:TextBox>  
  59.                                     </EditItemTemplate>  
  60.                                 </asp:TemplateField>  
  61.                             </Columns>  
  62.                         </asp:GridView>  
  63.                     </ItemTemplate>  
  64.                 </asp:TemplateField>  
  65.             </Columns>  
  66.         </asp:GridView>  
  67.     </div>  
  68.     </form>  
  69. </body>  
  70. </html>  
Your design will look like the following:

Design View

Write the following code in the aspx.cs page:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9.   
  10. public partial class Example : System.Web.UI.Page  
  11. {  
  12.     SqlConnection con = new SqlConnection("data source=RAHUL-PC\\RAHUL_PC;initial catalog=Example;Integrated security=true;"); //Create sqlconnection  
  13.   
  14.     SqlDataAdapter da;  
  15.     DataTable dt, dt1;  
  16.   
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.         if (!IsPostBack)  
  20.         {  
  21.             LoadGVLeader();  
  22.         }  
  23.     }  
  24.     protected void GVLeader_RowCommand(object sender, GridViewCommandEventArgs e)  
  25.     {  
  26.         int id = int.Parse(e.CommandName.ToString());  
  27.         int rowindex = int.Parse(e.CommandArgument.ToString());  
  28.         GridView GVMember = (GridView)GVLeader.Rows[rowindex].FindControl("GVMember");  
  29.         Button btn = ((Button)GVLeader.Rows[rowindex].FindControl("Button1"));  
  30.         if (btn.Text == "See Member")  
  31.         {  
  32.             btn.Text = "Cancel";  
  33.             GVMember.DataSource = LoadGVMember(id);  
  34.             GVMember.DataBind();  
  35.             GVMember.Visible = true;  
  36.         }  
  37.         else  
  38.         {  
  39.             btn.Text = "See Member";  
  40.             GVMember.Visible = false;  
  41.         }  
  42.   
  43.     }  
  44.     public void LoadGVLeader()  
  45.     {  
  46.         da = new SqlDataAdapter("Select * from TeamLeader", con);  
  47.         dt = new DataTable();  
  48.         da.Fill(dt);  
  49.         GVLeader.DataSource = dt;  
  50.         GVLeader.DataBind();  
  51.     }  
  52.     protected DataTable LoadGVMember(int id)  
  53.     {  
  54.         da = new SqlDataAdapter("select * from TeamMember where TLid=" + id, con);  
  55.         dt1 = new DataTable();  
  56.         da.Fill(dt1);  
  57.         return dt1;  
  58.     }  
  59.     protected void GVMember_RowEditing(object sender, GridViewEditEventArgs e)  
  60.     {  
  61.         GridView Gv = (GridView)sender;  
  62.         GridViewRow gvrow = (GridViewRow)Gv.Parent.Parent;  
  63.         int id = int.Parse(GVLeader.DataKeys[gvrow.RowIndex].Value.ToString());  
  64.         Gv.EditIndex = e.NewEditIndex;  
  65.         Gv.DataSource = LoadGVMember(id);  
  66.         Gv.DataBind();  
  67.     }  
  68.     protected void GVMember_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  69.     {  
  70.         GridView Gv = (GridView)sender;  
  71.         GridViewRow gvrow = (GridViewRow)Gv.Parent.Parent;  
  72.         int id = int.Parse(GVLeader.DataKeys[gvrow.RowIndex].Value.ToString());  
  73.         Gv.EditIndex = -1;  
  74.         Gv.DataSource = LoadGVMember(id);  
  75.         Gv.DataBind();  
  76.     }  
  77.     protected void GVMember_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  78.     {  
  79.         GridView Gv = (GridView)sender;  
  80.         GridViewRow gvrow = (GridViewRow)Gv.Parent.Parent;  
  81.         int id = int.Parse(GVLeader.DataKeys[gvrow.RowIndex].Value.ToString());  
  82.         int iid = int.Parse(Gv.DataKeys[e.RowIndex].Value.ToString());  
  83.         string Name = ((TextBox)(Gv.Rows[e.RowIndex].FindControl("TextBox4"))).Text.Trim();  
  84.         string Age = ((TextBox)(Gv.Rows[e.RowIndex].FindControl("TextBox5"))).Text.Trim();  
  85.         string Branch = ((TextBox)(Gv.Rows[e.RowIndex].FindControl("TextBox6"))).Text.Trim();  
  86.         da = new SqlDataAdapter("Update TeamMember set MemberName='" + Name + "',Age='" + Age + "',Branch='" + Branch + "' where iid=" + iid, con);  
  87.         dt1 = new DataTable();  
  88.         da.Fill(dt1);  
  89.         Gv.EditIndex = -1;  
  90.         Gv.DataSource = LoadGVMember(id);  
  91.         Gv.DataBind();  
  92.     }  
  93. }  
Now run the website and you will see the following output:

Grid view
When you click on “See Member", then you will see the following:

See Member

Now you can update the team member result on an Edit button click.

edit button click

And after clicking Update, the following is the result:

update member


Similar Articles