Home > ASP.Net > Display Master and Details output with Nested GridView Controls

Display Master and Details output with Nested GridView Controls


Hi try this example to Display Master and Details output with Nested GridView Contro

In this Example i will use Northwind Database .. you can also get it from  here to display Categories and it’s Related Product with Nested Gridview Control

1) Open VS2005 and Create a New Website and Add new Page “Default.aspx” so our page will be like this :-

<asp:GridView ID="MainGridView" runat="server" AutoGenerateColumns="False"
   CellPadding="6" Width="50%" Font-Names="Arial" GridLines="None" BorderColor="#000000"
   BorderStyle="Solid" BorderWidth="1px">
   <Columns>
      <asp:TemplateField HeaderText="Categories and Products">
         <ItemTemplate>
            <asp:Label runat="server" ID="lblCategoryName" Text='<%# Eval("CategoryName") %>'
               Font-Bold="True" Font-Size="12pt" />
            <asp:GridView ID="InnerGridView" runat="server" CellPadding="4" Width="100%"
               DataSource='<%# GetProductsByCategory(Eval("CategoryID").ToString()) %>'
               BorderStyle="Solid" BorderWidth="1px" Font-Size="8pt"
               BorderColor="#336699" GridLines="Horizontal">
               <HeaderStyle BackColor="#336699" Font-Bold="True" ForeColor="White" />
            </asp:GridView>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
   <HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="White" Font-Size="10pt" />
</asp:GridView>

and in Code behind we are going to bind Main Gridview with All Categories and then Bind Inner Gridview with Products related to Each Category so our code will be :-


protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
      LoadCategories();
  }
}

private void LoadCategories()
{
   string constr = @"Server=MyServer;Database=NORTHWND;Integrated Security=True;";
   string query = "SELECT TOP 3 CategoryID, CategoryName FROM Categories";
   SqlDataAdapter da = new SqlDataAdapter(query, constr);
   DataTable table = new DataTable();
   da.Fill(table);
   MainGridView.DataSource = table;
   MainGridView.DataBind();
}

public DataTable GetProductsByCategory(string categoryId)
{
  string constr = @"Server=MyServer;Database=NORTHWND;Integrated Security=True;";
  string query = "SELECT Top 3 ProductID, ProductName, UnitPrice FROM Products WHERE CategoryID = " + categoryId;
  SqlDataAdapter da = new SqlDataAdapter(query, constr);
  DataTable table = new DataTable();
  da.Fill(table);
  return table;
}

Hope this helps

Good Luck and Happy New Year 2011 🙂

Advertisements
Categories: ASP.Net
  1. October 2, 2011 at 1:31 pm

    hello!,I really like your writing very much! proportion we be in contact more approximately your post on AOL? I need a specialist in this area to unravel my problem. May be that’s you! Having a look ahead to look you.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: