Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to use nested GridView control in ASP.NET

Author: Nikki Smith
Download Source Code : 1345_NestedGridView.zip

In this article, I will show you how you can use nested GridView control in ASP.NET.

 

Nested GridView means a GridView within a GridView. We can add a child GridView in ItemTemplate of master GridView to show detail for the item in master GridView. It will give a different look to your data and it can be very useful in some cases. We can bind data to child GridView using RowDataBound or RowCommand event. I will use RowDataBound event to show products in child GridView for every category in master GridView row.

  1. Create a new Empty Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add a Web Form in the Web Site. No Need to change name of the Page
  3. Write below code in Web Form

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
        <Columns>
             <asp:TemplateField HeaderText="Category ID">
                <ItemTemplate>
                    <asp:Label ID="lblCatID" runat="server" Text='<%# Eval("CategoryID") %>' ></asp:Label>
                </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Category Name">
                <ItemTemplate>
                    <asp:Label ID="lblCatName" runat="server" Text='<%# Eval("CategoryName") %>' ></asp:Label>
                </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Products">
                <ItemTemplate>
                    <asp:Button ID="btnProducts" runat="server" Visible="false" CommandArgument='<%# Eval("CategoryID") %>' />
                    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField HeaderText="Product ID">
                                <ItemTemplate>
                                    <asp:Label ID="lblProdID" runat="server" Text='<%# Eval("ProductID") %>' ></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Product Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblProdName" runat="server" Text='<%# Eval("ProductName") %>' ></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Unit Price">
                                <ItemTemplate>
                                    <asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice") %>' ></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

     
  4. Namespaces used in the code

    Visual Basic
     
    Imports System.Data
    Imports System.Data.SqlClient
     
    Visual C#

    using System.Data;
    using System.Data.SqlClient;
     
  5. Write below code in Page Load method

    In below code, I have get data from Categories table of NORTHWIND database by setting connection to it. The DataSet object is filled using SqlDataAdapter. GridView1 is bound to DataSet.
     
    Visual Basic
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim text As String = "SELECT CategoryID, CategoryName FROM Categories"
        Dim connString As String = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True"
        Dim conn As New SqlConnection(connString)
        Dim cmd As New SqlCommand(text, conn)
        conn.Open()
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        conn.Close()
     
        GridView1.DataSource = ds
        GridView1.DataBind()
    End Sub
     
    Visual C#

    protected void Page_Load(object sender, EventArgs e)
    {
        string text = "SELECT CategoryID, CategoryName FROM Categories";
        string connString = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(text, conn);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
     
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
     
  6. Write below code in RowDataBound event of GridView1

    Category ID is retrieved from commandArgument attribute of button. We have to find child GridView to bind data to it. I have get data from Products table of NORTHWIND database against Category ID of every row of master GridView. Child GridView is bound to DataSet of products for every Category.
     
    Visual Basic
     
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
       If e.Row.RowType = DataControlRowType.DataRow Then
            Dim btn As Button = CType(e.Row.FindControl("btnProducts"), Button)
            Dim catID As Integer = Convert.ToInt32(btn.CommandArgument)
     
            Dim GridView2 As GridView = CType(e.Row.FindControl("GridView2"), GridView)
     
            Dim text As String = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE CategoryID=" & catID
            Dim connString As String = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True"
            Dim conn As New SqlConnection(connString)
            Dim cmd As New SqlCommand(text, conn)
            conn.Open()
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet()
            da.Fill(ds)
            conn.Close()
     
            GridView2.DataSource = ds
            GridView2.DataBind()
        End If
    End Sub

    Visual C#
     
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btn = (Button)e.Row.FindControl("btnProducts");
            int catID = Convert.ToInt32(btn.CommandArgument);
     
            GridView GridView2 = (GridView)e.Row.FindControl("GridView2");
     
            string text = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE CategoryID=" + catID;
            string connString = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(text, conn);
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            conn.Close();
     
            GridView2.DataSource = ds;
            GridView2.DataBind();
        }
    }
     
  7. Now you can see website in your browser

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to export DataTable to Word in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net