Nested GridView in ASP.NET
The GridView control is the successor to the DataGrid and extends it in a number of ways. In some situations we have to display data in a Master-Child manner. In the following article, you can see how to create a GridView control to display data in a Matser-Child way.

Database
In this article I have used Microsoft's Pubs database for sample data. You can download it free from the following link.
How to nested Gridview Control
A GridView control inside the grid row of the parent GridView control is called a nested GridView. Here the program displays Master data from Publisher table and displays Child data from Titles table.
Expand/Collapse
Here the Gridview show Master/Details in Expand/Collapse way. When you click on + symbol the Gridview expand the row and inside the row it display the child Gridview. Then the row display a - symbol and when you click on the - symbol the it collapse and show the master GridView only.

Here the program manage this expand/collapse using two images and it functioning with the support of a small jQuery function.
Default.aspx
Gridview inside another Gridview
C# Source Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; public partial class NestedGridView : System.Web.UI.Page { string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****"; protected void Page_Load(object sender, EventArgs e) { string sql = "SELECT pub_id, pub_name,state,country FROM publishers"; GridView1.DataSource = getData(sql); GridView1.DataBind(); } protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string pub_id = GridView1.DataKeys[e.Row.RowIndex].Value.ToString(); string sql = "SELECT pub_id, title, type,price FROM titles WHERE pub_id='" + pub_id + "'"; GridView pubTitle = (GridView)e.Row.FindControl("GridView2"); pubTitle.DataSource = getData(sql); pubTitle.DataBind(); } } private DataTable getData(string sql) { SqlDataAdapter adapter = new SqlDataAdapter(); DataTable dTable = new DataTable(); SqlConnection connection = new SqlConnection(connetionString); connection.Open(); SqlCommand command = new SqlCommand(sql, connection); adapter.SelectCommand = command; adapter.Fill(dTable); adapter.Dispose(); command.Dispose(); connection.Close(); return dTable; } }
VB.Net Source Code
Imports System.Drawing Imports System.Data.SqlClient Imports System.Data Partial Class _Default Inherits System.Web.UI.Page Dim connetionString As String = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=zen412" Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim sql As String = "SELECT pub_id, pub_name,state,country FROM publishers" GridView1.DataSource = getData(sql) GridView1.DataBind() End Sub Protected Sub GridView1_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim pub_id As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString() Dim sql As String = (Convert.ToString("SELECT pub_id, title, type,price FROM titles WHERE pub_id='") & pub_id) + "'" Dim pubTitle As GridView = DirectCast(e.Row.FindControl("GridView2"), GridView) pubTitle.DataSource = getData(sql) pubTitle.DataBind() End If End Sub Private Function getData(ByVal sql As String) As DataTable Dim adapter As New SqlDataAdapter() Dim dTable As New DataTable() Dim connection As New SqlConnection(connetionString) connection.Open() Dim command As New SqlCommand(sql, connection) adapter.SelectCommand = command adapter.Fill(dTable) adapter.Dispose() command.Dispose() connection.Close() Return dTable End Function End Class
- ASP.NET Simple GridView
- Sorting , Paging and AutoGenerateColumns
- ASP.NET GridView Editing
- ASP.NET GridView Delete
- DropDownList in GridView
- Create Gridview at runtime
- Gridview - Add, Edit and delete
- Gridview export to Excel
- Gridview export to CSV
- GridView summary on Footer
- Subtotal row in Gridview
- SubTotal and GrandTotal in GridView
- Create Gridview without database
- GridView from Stored Procedure
- How to create Gridview Popup Form
- How to create a Blank row in gridview
- Freeze Gridview Header row, Scroll in GridView
- Auto Generate Row Number in GridView
- Image between GridView rows
- How to select a row in gridview
- Checkbox in ASP.NET GridView Control