SiteMap  | About    

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.

nested gridview


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.


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.

expand collapse gridview

Here the program manage this expand/collapse using two images and it functioning with the support of a small jQuery function.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
	<title>Gridview inside another Gridview</title>
	<script type="text/javascript" src=""></script>
    <script type="text/javascript">
	    function shrinkandgrow(input) {
	        var displayIcon = "img" + input;
	        if ($("#" + displayIcon).attr("src") == "grow.png")
                $("#" + displayIcon).closest("tr")
			    .after("<tr><td></td><td colspan = '100%'>" + $("#" + input)
			    .html() + "</td></tr>");
                $("#" + displayIcon).attr("src", "shrink.png");
            } else
                $("#" + displayIcon).closest("tr").next().remove();
                $("#" + displayIcon).attr("src", "grow.png");
	<form id="form1" runat="server">
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="pub_id"
		OnRowDataBound="GridView1_OnRowDataBound" HeaderStyle-BackColor="#A52A2A" HeaderStyle-ForeColor="White" >
		<asp:TemplateField ItemStyle-Width="20px">
			<a href="JavaScript:shrinkandgrow('div<%# Eval("pub_id") %>');">
				<img alt="Details" id="imgdiv<%# Eval("pub_id") %>" src="grow.png" />
			<div id="div<%# Eval("pub_id") %>" style="display: none;">
				<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" DataKeyNames="pub_id"
                HeaderStyle-BackColor="#FFA500" HeaderStyle-ForeColor="White">
					<asp:BoundField ItemStyle-Width="150px" DataField="title" HeaderText="Title" />
					<asp:BoundField ItemStyle-Width="100px" DataField="type" HeaderText="Category" />
					<asp:BoundField ItemStyle-Width="100px" DataField="price" HeaderText="Price" />
			<asp:BoundField ItemStyle-Width="150px" DataField="pub_name" HeaderText="Publisher" />
			<asp:BoundField ItemStyle-Width="100px" DataField="state" HeaderText="State" />
			<asp:BoundField ItemStyle-Width="100px" DataField="country" HeaderText="Country" />

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);
    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);
    private DataTable getData(string sql)
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataTable dTable = new DataTable();
        SqlConnection connection = new SqlConnection(connetionString);
        SqlCommand command = new SqlCommand(sql, connection);
        adapter.SelectCommand = command;
        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)
    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)
        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)
        Dim command As New SqlCommand(sql, connection)
        adapter.SelectCommand = command
        Return dTable
    End Function
End Class (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.