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.
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.
- Enter new row/ record using footer of a template in outer as well as inner GridView.
- Edit, Update, Delete record/rows of particular GridView.
- Collapsible inner GridView for every row of an outer GridView i.e subset of every record.
Design Outer GridView
- <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">
- <Columns>
- <asp:TemplateField HeaderText="Name">
- <ItemTemplate>
- <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>' /> </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="txtEditName" runat="server" placeholder="Enter Name Please" Text='<%# Eval("Name")%>' /> </EditItemTemplate>
- <FooterTemplate>
- <asp:TextBox ID="txtName" runat="server" placeholder="Enter Name Please" /> </FooterTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Action">
- <ItemTemplate>
- <asp:LinkButton runat="server" ID="btnEdit" CommandName="Edit" ToolTip="Edit" Text="Edit" CssClass=""><i class="fa fa-x fa-edit"></i></asp:LinkButton>
- <asp:LinkButton runat="server" ID="btndelete" ToolTip="Remove" CommandName="DeleteParentRow" Text="Delete" CssClass=""><span class="fa fa-x fa-close"></span></asp:LinkButton>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:LinkButton runat="server" ID="btnSubUpdate" CommandName="Update" ToolTip="Update" Text="Update" CssClass=""><i class="fa fa-x fa-thumbs-up"></i></asp:LinkButton>
- <asp:LinkButton runat="server" ID="btnCancel" ToolTip="Cancel" CommandName="Cancel" Text="Cancel" CssClass=""><i class="fa fa-x fa-thumbs-down"></i></asp:LinkButton>
- </EditItemTemplate>
- <FooterTemplate>
- <asp:Button ID="btnSubAdd" CommandName="AddNew" runat="server" Text="Add New Record" CssClass="btn btn-sm btn-primary" /> </FooterTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>

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.
- <asp:TemplateField>
- <ItemTemplate>
- <tr>
- <td>
- <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">
- <Columns>
- <asp:TemplateField HeaderText="Name">
- <ItemTemplate>
- <asp:Label ID="lblSubName" runat="server" Text='<%# Eval("SubName")%>' /> </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="txtSubEditName" runat="server" placeholder="Enter child Name Please" Text='<%# Eval("SubName")%>' /> </EditItemTemplate>
- <FooterTemplate>
- <asp:TextBox ID="txtSubName" runat="server" placeholder="Enter child Name Please" /> </FooterTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Action">
- <ItemTemplate>
- <asp:LinkButton runat="server" ID="btnChildEdit" CommandName="Edit" ToolTip="Edit" Text="Edit" CssClass=""><i class="fa fa-x fa-edit"></i></asp:LinkButton>
- <asp:LinkButton runat="server" ID="btnChilddelete" ToolTip="Remove" CommandName="DeleteParentRow" Text="Delete" CssClass=""><span class="fa fa-x fa-close"></span></asp:LinkButton>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:LinkButton runat="server" ID="btnChildUpdate" CommandName="Update" ToolTip="Update" Text="Update" CssClass=""><i class="fa fa-x fa-thumbs-up"></i></asp:LinkButton>
- <asp:LinkButton runat="server" ID="btnChildCancel" ToolTip="Cancel" CommandName="Cancel" Text="Cancel" CssClass=""><i class="fa fa-x fa-thumbs-down"></i></asp:LinkButton>
- </EditItemTemplate>
- <FooterTemplate>
- <asp:Button ID="btnChildAdd" CommandName="AddNew" runat="server" Text="Add New Record" CssClass="btn btn-sm btn-primary" /> </FooterTemplate>
- </asp:TemplateField>
- </Columns>
- <td>
- </tr>
- </ItemTemplate>
- </asp:TemplateField>

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
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
Code Snippet
- if (e.Row.RowType == DataControlRowType.DataRow) // Fill or load child gridviews for every parent grid row
- {
- GridView gv = (GridView) e.Row.FindControl("gvChildGrid");
- Find gridview
- for every Row
- DataTable dtAction = new DataTable();
- dtAction = LoadSubCORList(Convert.ToString(id));
- if (dtAction.Row.Count > 0) {
- gv.DataSource = dtAction
- gv.DataBind();
- } else {
- // If there is no sub set for row then return empty grid to add new row
- dtAction.Rows.Add(dtAction.NewRow()); // Add new row to empty datatable
- gv.DataSource = dtAction;
- gv.DataBind();
- int columncount = gv.Rows[0].Cells.Count;
- gv.Rows[0].Cells.Clear();
- gv.Rows[0].Cells.Add(new TableCell());
- gv.Rows[0].Cells[0].ColumnSpan = columncount;
- gv.Rows[0].Cells[0].Text = "No Records Found";
- }
- }
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.
The following code was used in Child GridView events to identify the functioning GridView.

Code Snippets
- GridView GV = (GridView)sender; // Select either of inner gridview who request for Edit / cancel edit out of all innner gridview
- 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
Script For Collapsible Events
- <script language="javascript" type="text/javascript">
- function divexpandcollapse(divname) {
- var div = document.getElementById(divname);
- var img = document.getElementById('img' + divname);
- if (div.style.display == "none") {
- div.style.display = "inline";
- img.src = "../images/minus.png";
- } else {
- div.style.display = "none";
- img.src = "../images/plus.png";
- }
- }
- </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.
Add the below template to add collapsible option to parent GridView and child GridView will be hidden/shown for every row.
- <asp:TemplateField ItemStyle-Width="20px">
- <ItemTemplate> <a href="JavaScript:divexpandcollapse('div<%# Eval(" ID ") %>');">
- <img id="imgdiv<%# Eval("ID") %>" width="14px" border="0" src="../images/plus.png" />
- </a> </ItemTemplate>
- </asp:TemplateField>

Cesar NegretePosted Aug 2, 2024, 9:32 PM
Dear Sagar please You share your code at [email protected]
Niravkumar VaghelaPosted Jul 1, 2024, 10:44 AM
Dear, Sagar Can You Please Share code. It will helpful for me.
News EagerPosted Jul 15, 2023, 7:54 AM
LoadsubCoRlist
Bryan DuemlerPosted Mar 15, 2023, 1:17 AM
Dear Sagar Can You Please Share code to my mail - [email protected] Thank You
Woodley WestbrookPosted Oct 18, 2021, 7:54 PM
Dear Sagar Can You Please Share code on my mail - [email protected]
supriya patilPosted Sep 28, 2021, 7:04 AM
Please mail code to [email protected]
Ripon GogiPosted Sep 8, 2021, 3:48 PM
Can you please send me the source code like which would be great for me. My email is [email protected]. Thanks
Agusta PradnyanaPosted Sep 8, 2021, 7:24 AM
Hai, Can you send to me your complate code please? [email protected].. thanks
Social UsePosted Aug 30, 2021, 4:24 AM
Can I have the code pls? [email protected], thankyou
supriya gadePosted Aug 23, 2021, 4:13 PM
Hi can you plz send all code to [email protected]
jay singhPosted Mar 22, 2021, 4:37 AM
It is looking very Good but not working for me plz help
jay singhPosted Mar 22, 2021, 4:36 AM
Dear Sagar Can You Please Share code on my mail Id [email protected].
Shahul HameedPosted Mar 16, 2021, 8:19 AM
Hello Author, Could you please share your code to my mail Id: [email protected]. It is very helpful for me.
kumari reemaPosted Nov 11, 2020, 1:48 AM
Would you please share the full code. to [email protected], Thanks
Dale CallowPosted Aug 7, 2020, 11:57 AM
Could you please share your code for this, thank you!
Mamta MestryPosted Jul 22, 2020, 10:17 AM
Can u please help us for freezing columns in nested grid.
Hoang LePosted May 26, 2020, 3:02 AM
Would you please share the full code to [email protected]. thank
Ashok KPosted May 21, 2020, 12:22 AM
Kindly share your code to [email protected]? Thanks in Advance!
Dinkar HirePosted May 7, 2020, 5:16 AM
Would you please share the full code. to [email protected], Thanks
William BittenbenderPosted Apr 2, 2020, 8:57 AM
Are you not sharing your code?
William BittenbenderPosted Mar 26, 2020, 11:47 AM
I am having issues keeping the inner grid row open to perform operations....I was really hoping to see your code.
William BittenbenderPosted Mar 25, 2020, 9:14 AM
Would you be so kind as to share the full code... [email protected]? Thank you very much
Nguyen LanPosted Mar 23, 2020, 3:17 AM
I saw your code Design Outer GridView/ Design Inner GridView that conflict your picture (your picture has multiple columns but your code one column). Why???
Nguyen LanPosted Mar 16, 2020, 1:17 PM
Can you please share your code to [email protected]? Thanks!
Anand NavalePosted Jan 5, 2020, 11:46 PM
Nice Explanation ,Great job
David ManorajPosted Dec 19, 2019, 2:21 AM
Dear, Please Share code [email protected]
sam kothariPosted Oct 18, 2019, 6:03 AM
Dear Sagar, Please share working code with me on [email protected]
asad ullahPosted Oct 1, 2019, 12:03 PM
Please share working code with me on [email protected]
sidra InamPosted Oct 1, 2019, 12:55 AM
Please share your code at [email protected]
sagayam nithyaPosted Aug 16, 2019, 4:29 AM
Hi Sagar, Excellent work. Please can u email me complete code. [email protected]
Anthony EarlyPosted Jul 17, 2019, 10:02 AM
Can you please share your code to [email protected]? Thanks
Ever GuzmánPosted Jun 27, 2019, 11:51 AM
Can you please share your code to [email protected]?
Amornkan ElfPosted Jun 9, 2019, 11:34 PM
Please shear your code [email protected]
Manish GanatraPosted Jun 3, 2019, 4:49 AM
Hi Sagar, this meets my requirement, can u Please email me complete code. [email protected]
Sheikh FareethPosted May 14, 2019, 12:51 AM
Hello sagar please send the nested gridview in mvc.5 using with three tables with edit and delete option to [email protected]
Mohammed AfrozPosted Apr 25, 2019, 6:18 AM
Hi Sagar, Nice work, this meets my requirement, Can you please share your code to my email. [email protected]
manjunath matolliPosted Apr 9, 2019, 12:18 AM
Please shear your code [email protected]
Warren CoxPosted Feb 1, 2019, 10:40 AM
Hello Sagar please send me code for Nested GridView In ASP.NET Using C# (Parent And Child Gridview) to [email protected] thank you
IsaiSelvan SPosted Jan 9, 2019, 1:37 AM
In case there is no record in datatable or SQL , how grid will be binded on page load with header and first empty row?
IsaiSelvan SPosted Jan 9, 2019, 1:36 AM
Hi Sagar, Excellent work. Thank you very much. Please can u email me complete code. [email protected]
Sheikh FareethPosted Nov 21, 2018, 3:32 AM
Hi Sagar Can you please send the full project code
Rajani BijapurPosted Nov 16, 2018, 5:53 AM
Hi... can you share me the complete source code to [email protected]
ruel luengoPosted Oct 28, 2018, 3:27 AM
Please can you send the complete to my eamil please [email protected]
Daniel AlvarezPosted Oct 11, 2018, 6:42 AM
Hello Sagar! Great work! Can you share the code for something similar?
Carlos SantoyoPosted Sep 28, 2018, 11:38 AM
Can I get your code please?
pradeep dasariPosted Aug 8, 2018, 6:57 AM
Hi sagar, all program is fine but i need database for above program
Bansi HPosted Jun 11, 2018, 2:35 AM
Hi Sagar , can you share source code for same