Download Source File

Introduction

This example shows you how you create Nested GridView i.e. GridView under another GridView using little JQuery Code. Lets explain.

 Nested GridView

Background

I'm using Northwind Database here.

Quick Start

In Master GridView I'll show you the Customer's details i.e. ContactName and City from 'Customer' table. Then in the Child GridView I'll show you Order details of corresponding Customers i.e. OrderID and OrderDate.

Creating Main GridView

01.<asp:GridView
02.    ID="grdViewCustomers"
03.    runat="server"
04.    AutoGenerateColumns="false"
05.    DataKeyNames="CustomerID"
06.    OnRowDataBound="grdViewCustomers_OnRowDataBound"
07.    CssClass="Grid">
08.    <Columns>
09.        <asp:BoundField
10.            ItemStyle-Width="150px"
11.            DataField="ContactName"
12.            HeaderText="Contact Name" />
13.        <asp:BoundField
14.            ItemStyle-Width="150px"
15.            DataField="City"
16.            HeaderText="City" />
17.    </Columns>
18.</asp:GridView>

So, now it shows only two column i.e. ContactName and City. Now, I'll insert 'plus sign image' to the First Column of every row. As because, when I'll click this 'plus sign image' then the Child GridView will displayed and the 'plus sign image' will be the 'minus sign image'. And when I'll click the 'minus sign image' the Child GridView will remove from our sight and 'minus sign image' becomes the 'plus sign image' like toggle. So, I've to take an ItemTemplate within a TemplateField inside the Columns at first position. Let see:

01.<asp:GridView
02.    ID="grdViewCustomers"
03.    runat="server"
04.    AutoGenerateColumns="false"
05.    DataKeyNames="CustomerID"
06.    OnRowDataBound="grdViewCustomers_OnRowDataBound"
07.    CssClass="Grid">
08.    <Columns>
09.        <!-- Inserting 'plus sign' -->
10.        <asp:TemplateField ItemStyle-Width="20px">
11.            <ItemTemplate>
12.                <ahref="JavaScript:divexpandcollapse('div<%# Eval("CustomerID") %>');">
13.                    <imgalt="Details" id="imgdiv<%# Eval("CustomerID") %>" src="images/plus.png" />
14.                </a>
15.            </ItemTemplate>
16.        </asp:TemplateField>
17.        <asp:BoundField
18.            ItemStyle-Width="150px"
19.            DataField="ContactName"
20.            HeaderText="ContactName" />
21.        <asp:BoundField
22.            ItemStyle-Width="150px"
23.            DataField="City"
24.            HeaderText="City"/>
25.    </Columns>
26.</asp:GridView>

Now, you see that I've linked a JavaScript function to the 'plus sign image' which does the all functionality what I've told above against the Click event of the 'plus sign image'. This JavaScript function takes the Div name in which the Child GridView exists. There will be one Child GridView for each row of the Master GridView. So, the Div id must be different. That's why I concatenate Div id with CustomerID and there will one 'plus sign image' for each row of the Master GridView, so I also concatenate the img id with CustomerID. Now lets add the Div just after the link of the 'plus sign image' and implement Child GridView under that Div:

01.<asp:GridView
02.    ID="grdViewCustomers"
03.    runat="server"
04.    AutoGenerateColumns="false"
05.    DataKeyNames="CustomerID"
06.    OnRowDataBound="grdViewCustomers_OnRowDataBound"
07.    CssClass="Grid">
08.    <Columns>
09.        <!-- Inserting 'plus sign' -->
10.        <asp:TemplateField ItemStyle-Width="20px">
11.            <ItemTemplate>
12.                <a href="JavaScript:divexpandcollapse('div<%# Eval("CustomerID") %>');">
13.                    <img alt="Details" id="imgdiv<%# Eval("CustomerID") %>" src="images/plus.png" />
14.                </a>
15.                <!-- Adding the Div container -->
16.                <div id="div<%# Eval("CustomerID") %>" style="display: none;">
17.                    <!-- Adding Child GridView -->
18.                    <asp:GridView
19.                        ID="grdViewOrdersOfCustomer"
20.                        runat="server"
21.                        AutoGenerateColumns="false"
22.                        DataKeyNames="CustomerID"
23.                        CssClass="ChildGrid">
24.                        <Columns>
25.                            <asp:BoundField
26.                                ItemStyle-Width="150px"
27.                                DataField="OrderID"
28.                                HeaderText="Order ID" />
29.                            <asp:BoundField
30.                                ItemStyle-Width="150px"
31.                                DataField="OrderDate"
32.                                HeaderText="Order Date" />
33.                        </Columns>
34.                    </asp:GridView>
35.                </div>
36.            </ItemTemplate>
37.        </asp:TemplateField>
38.        <asp:BoundField
39.            ItemStyle-Width="150px"
40.            DataField="ContactName"
41.            HeaderText="Contact Name" />
42.        <asp:BoundField
43.            ItemStyle-Width="150px"
44.            DataField="City"
45.            HeaderText="City" />
46.    </Columns>
47.</asp:GridView>

Lets see the little JQuery which checks whether the 'plus sign image' source contains the path of the 'plus sign' image or 'minus sign' image and do said functionality accordingly:

01.function divexpandcollapse(divname) {
02.    var img = "img" + divname;
03.    if ($("#" + img).attr("src") == "images/plus.png") {
04.        $("#" + img)
05.            .closest("tr")
06.            .after("<tr><td></td><td colspan = '100%'>" + $("#" + divname)
07.            .html() + "</td></tr>")
08.        $("#" + img).attr("src", "images/minus.png");
09.    } else {
10.        $("#" + img).closest("tr").next().remove();
11.        $("#" + img).attr("src", "images/plus.png");
12.    }
13.}

Now the client side part is over. The main part is how you fill the Child GridView? Don't worry, there is an event which is triggered when there is one container control within the row of the GridView. The event is OnRowDataBound. And I've already added this event to the Master GridView properties and the name of event handler is: grdViewCustomers_OnRowDataBound. And we also fill the Master GridView in the Page_Load() event. So lets implement:

01.protected void grdViewCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
02.{
03.    if (e.Row.RowType == DataControlRowType.DataRow)
04.    {
05.        string customerID = grdViewCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
06.        GridView grdViewOrdersOfCustomer = (GridView)e.Row.FindControl("grdViewOrdersOfCustomer");
07.        grdViewOrdersOfCustomer.DataSource = SelectData("SELECT top 3 CustomerID, OrderID, OrderDate FROM Orders WHERE CustomerID='" + customerID + "'");
08.        grdViewOrdersOfCustomer.DataBind();
09.    }
10.}
11. 
12.private DataTable SelectData(string sqlQuery)
13.{
14.    string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SQLServerConnectionString"].ConnectionString;
15.    using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery, connectionString))
16.    {
17.        DataTable dt = new DataTable("Customers");
18.        sqlDataAdapter.Fill(dt);
19.        return dt;
20.    }
21.}
22. 
23.protected void Page_Load(object sender, EventArgs e)
24.{
25.    grdViewCustomers.DataSource = SelectData("SELECT top 3 CustomerID, ContactName, City FROM Customers");
26.    grdViewCustomers.DataBind();
27.}

Its Done.

History

This is the first release of this Article. In the next release I'll show you how you add a button in nested GridView and how the whole thing can be done using AJAX. Thank You.

If you've any doubt, please post your questions. If you really like this article, please share it.

Don't forget to Vote or Comment about my writing.

See Also