Nested GridView In ASP.NET Using C# (Parent And Child Gridview)

Nested GridView and Crud Operation

Working on single GridView with all CRUD operations is a tedious task to implement but not difficult.

If we need to implement in nested form then it makes it more complex compared to single GridView. In the below case, I have implemented a nested GridView where the following provision is made available.
  1. Enter new row/ record using footer of a template in outer as well as inner GridView.
  2. Edit, Update, Delete record/rows of particular GridView.
  3. Collapsible inner GridView for every row of an outer GridView i.e subset of every record.
Design Outer GridView
  1. <asp:GridView ID="grdParent" AutoGenerateColumns="false" ShowFooter="true" EmptyDataText="No Records Found" CssClass="table table-bordered" runat="server" OnRowDataBound="grdParent_RowDataBound" OnRowCancelingEdit="grdParent_RowCancelingEdit" OnRowCommand="grdParent_RowCommand" OnRowEditing="grdParent_RowEditing" OnRowUpdating="grdParent_RowUpdating">  
  2.     <Columns>  
  3.         <asp:TemplateField HeaderText="Name">  
  4.             <ItemTemplate>  
  5.                 <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>' /> </ItemTemplate>  
  6.             <EditItemTemplate>  
  7.                 <asp:TextBox ID="txtEditName" runat="server" placeholder="Enter Name Please" Text='<%# Eval("Name")%>' /> </EditItemTemplate>  
  8.             <FooterTemplate>  
  9.                 <asp:TextBox ID="txtName" runat="server" placeholder="Enter Name Please" /> </FooterTemplate>  
  10.         </asp:TemplateField>  
  11.         <asp:TemplateField HeaderText="Action">  
  12.             <ItemTemplate>  
  13.                 <asp:LinkButton runat="server" ID="btnEdit" CommandName="Edit" ToolTip="Edit" Text="Edit" CssClass=""><i class="fa fa-x fa-edit"></i></asp:LinkButton>     
  14.                 <asp:LinkButton runat="server" ID="btndelete" ToolTip="Remove" CommandName="DeleteParentRow" Text="Delete" CssClass=""><span class="fa fa-x fa-close"></span></asp:LinkButton>  
  15.             </ItemTemplate>  
  16.             <EditItemTemplate>  
  17.                 <asp:LinkButton runat="server" ID="btnSubUpdate" CommandName="Update" ToolTip="Update" Text="Update" CssClass=""><i class="fa fa-x fa-thumbs-up"></i></asp:LinkButton>     
  18.                 <asp:LinkButton runat="server" ID="btnCancel" ToolTip="Cancel" CommandName="Cancel" Text="Cancel" CssClass=""><i class="fa fa-x fa-thumbs-down"></i></asp:LinkButton>  
  19.             </EditItemTemplate>  
  20.             <FooterTemplate>  
  21.                 <asp:Button ID="btnSubAdd" CommandName="AddNew" runat="server" Text="Add New Record" CssClass="btn btn-sm btn-primary" /> </FooterTemplate>  
  22.         </asp:TemplateField>  
  23.     </Columns>  
  24. </asp:GridView>  
ASP.NET

Simple Design for outer GridView where fields are bound within column tag using item template.

Edit template is used for editing and updating rows.

FooterTemplate is used as a provision to enter a new record to grid at the bottom of the GridView.

We need to mention footer template to every field of a row where there is a need to enter a new value.

Design Inner GridView

Add one more item template at the end of parent GridView which will replicate or generate a child GridView for every bound row of parent grid.
  1. <asp:TemplateField>  
  2.     <ItemTemplate>  
  3.         <tr>  
  4.             <td>  
  5.                 <asp:GridView ID="gvChildGrid" CssClass="table table-bordered" ShowFooter="true" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvChildGrid_RowDataBound" OnRowEditing="gvChildGrid_RowEditing" OnRowCancelingEdit="gvChildGrid_RowCancelingEdit" OnRowCommand="gvChildGrid_RowCommand" OnRowUpdating="gvChildGrid_RowUpdating">  
  6.                     <Columns>  
  7.                         <asp:TemplateField HeaderText="Name">  
  8.                             <ItemTemplate>  
  9.                                 <asp:Label ID="lblSubName" runat="server" Text='<%# Eval("SubName")%>' /> </ItemTemplate>  
  10.                             <EditItemTemplate>  
  11.                                 <asp:TextBox ID="txtSubEditName" runat="server" placeholder="Enter child Name Please" Text='<%# Eval("SubName")%>' /> </EditItemTemplate>  
  12.                             <FooterTemplate>  
  13.                                 <asp:TextBox ID="txtSubName" runat="server" placeholder="Enter child Name Please" /> </FooterTemplate>  
  14.                         </asp:TemplateField>  
  15.                         <asp:TemplateField HeaderText="Action">  
  16.                             <ItemTemplate>  
  17.                                 <asp:LinkButton runat="server" ID="btnChildEdit" CommandName="Edit" ToolTip="Edit" Text="Edit" CssClass=""><i class="fa fa-x fa-edit"></i></asp:LinkButton>     
  18.                                 <asp:LinkButton runat="server" ID="btnChilddelete" ToolTip="Remove" CommandName="DeleteParentRow" Text="Delete" CssClass=""><span class="fa fa-x fa-close"></span></asp:LinkButton>  
  19.                             </ItemTemplate>  
  20.                             <EditItemTemplate>  
  21.                                 <asp:LinkButton runat="server" ID="btnChildUpdate" CommandName="Update" ToolTip="Update" Text="Update" CssClass=""><i class="fa fa-x fa-thumbs-up"></i></asp:LinkButton>     
  22.                                 <asp:LinkButton runat="server" ID="btnChildCancel" ToolTip="Cancel" CommandName="Cancel" Text="Cancel" CssClass=""><i class="fa fa-x fa-thumbs-down"></i></asp:LinkButton>  
  23.                             </EditItemTemplate>  
  24.                             <FooterTemplate>  
  25.                                 <asp:Button ID="btnChildAdd" CommandName="AddNew" runat="server" Text="Add New Record" CssClass="btn btn-sm btn-primary" /> </FooterTemplate>  
  26.                         </asp:TemplateField>  
  27.                     </Columns>  
  28.                     <td>  
  29.         </tr>  
  30.     </ItemTemplate>  
  31. </asp:TemplateField>  
ASP.NET
 
This inner GridView will function with CRUD operations the same as outer GridView but it has the challenge to identify which inner GridView has been called for functionality.

Functionality for Parent Gridview
  • Protected void grdParent_RowDataBound: Columns get bind as per data source.
  • Protected void grdParent_RowCommand: Based on CommandName, a respective set of operation execute like AddNew Row, Delete Row.
  • Protected void grdParent_RowEditing: Edit particular rowindex of a GridView.
  • Protected void grdParent_RowUpdating: Execute Edit functionality for selected editing row.
  • Protected void grdParent_RowCancelingEdit: Cancel Edit functionality for any row.
Bind internal Grid in Parent RowdataBound event.

Code Snippet
  1. if (e.Row.RowType == DataControlRowType.DataRow) // Fill or load child gridviews for every parent grid row  
  2. {  
  3.     GridView gv = (GridView) e.Row.FindControl("gvChildGrid");  
  4.     Find gridview  
  5.     for every Row  
  6.     DataTable dtAction = new DataTable();  
  7.     dtAction = LoadSubCORList(Convert.ToString(id));  
  8.     if (dtAction.Row.Count > 0) {  
  9.         gv.DataSource = dtAction  
  10.         gv.DataBind();  
  11.     } else {  
  12.         // If there is no sub set for row then return empty grid to add new row  
  13.         dtAction.Rows.Add(dtAction.NewRow()); // Add new row to empty datatable  
  14.         gv.DataSource = dtAction;  
  15.         gv.DataBind();  
  16.         int columncount = gv.Rows[0].Cells.Count;  
  17.         gv.Rows[0].Cells.Clear();  
  18.         gv.Rows[0].Cells.Add(new TableCell());  
  19.         gv.Rows[0].Cells[0].ColumnSpan = columncount;  
  20.         gv.Rows[0].Cells[0].Text = "No Records Found";  
  21.     }  
  22. }  
Functionality for Child Gridview
  • protected void gvChildGrid_RowCommand : Based on CommandName, respective Set of operation execute like AddNew Row, Delete Row on particular inner grid.
  • protected void gvChildGrid_RowDataBound :Columns get bind as per datasource
  • protected void gvChildGrid_RowEditing: Edit particular rowindex of inner gridview
  • protected void gvChildGrid_RowCancelingEdit: Cancel Edit functionality for any row.
  • protected void gvChildGrid_RowUpdating : Execute Edit functionality for selected editing row.
In the above case, it's difficult to figure out which inner GridView is functioning at a time.

The following code was used in Child GridView events to identify the functioning GridView.

 ASP.NET
 
Code Snippets
  1. GridView GV = (GridView)sender; // Select either of inner gridview who request for Edit / cancel edit out of all innner gridview  
  2. GridViewRow grdParent = (GridViewRow)GridView2.Parent.Parent; // Find controls from parent grid view  
Use the above line to identify the inner grid view to perform selective operations on respective inner grid.

Script For Collapsible Events
  1. <script language="javascript" type="text/javascript">  
  2.     function divexpandcollapse(divname) {  
  3.         var div = document.getElementById(divname);  
  4.         var img = document.getElementById('img' + divname);  
  5.         if (div.style.display == "none") {  
  6.             div.style.display = "inline";  
  7.             img.src = "../images/minus.png";  
  8.         } else {  
  9.             div.style.display = "none";  
  10.             img.src = "../images/plus.png";  
  11.         }  
  12.     }  
  13. </script>  
Add the following itemtemplate for parent gridview

Add the below template to add collapsible option to parent GridView and child GridView will be hidden/shown for every row.
  1. <asp:TemplateField ItemStyle-Width="20px">  
  2.     <ItemTemplate> <a href="JavaScript:divexpandcollapse('div<%# Eval(" ID ") %>');">  
  3. <img id="imgdiv<%# Eval("ID") %>" width="14px" border="0" src="../images/plus.png" />  
  4. </a> </ItemTemplate>  
  5. </asp:TemplateField>  
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now