Click here to Skip to main content
15,867,834 members
Articles / Web Development / XHTML

Nest Gridviews using LinqDataSource

Rate me:
Please Sign up or sign in to vote.
4.29/5 (5 votes)
30 Sep 2008CPOL2 min read 30.3K   696   25  
Here, I will explain how to put Gridview in other one, such as categories and products.

Introduction

In this article, I will talk about creating two nested gridviews, using LinqDataSource and LinqToSql. This can be done in many ways, but this is a very simple way.

Let's take a look at the screenshot:

Linq2NestedGridView/screenshot.png

Using the Code

I will explain it step by step:

  1. Create LinqToSql Class and put two tables, Categories and Products:

    Linq2NestedGridView/picture1.png

  2. In WebPage, make new GridView and name it GridView1.

    Linq2NestedGridView/picture2.png

  3. Create LinqDataSource, and configure it to take CategoryName from Category Table.

    Linq2NestedGridView/picture4.png

    Till now, the gridview will be like that:

    Linq2NestedGridView/picture5.png

  4. In the previous steps, we create a GridView with one Column, and bind it with CategoryName.

    So we need to put another Gridview in the second column, this second Gridview will hold Products that are in category mentioned in first Column, so we will create TemplateField and put GridView in it.

    Linq2NestedGridView/picture6.png

    Then add GridView inside this field.

  5. Create another Linq DataSource and configure it as follows:

    Linq2NestedGridView/picture8.png

    Make DataSource take parameter CategoryID, we will give the datasource CategoryID programatically later.

    Linq2NestedGridView/picture13.png

    Here, the LinqDataSource will select Products according to CategoryID parameter which is identified as WhereParameter:

    Linq2NestedGridView/picture10.png

  6. At this point, we don't do anything unless we put GridView In the second column andccreate another LinqDataSource which gets products by category ID. In the next step, we want to give LinqDataSource the CategoryID of the categoryName in the First Column. So we get the CategoryID of the CategoryName of First Column and pass it to LinqDataSource as WhereParameter, this will occur in GridView RowCreated Event to pass update the LinqDataSource when each row is created and bind Gridview of that Row.

    Linq2NestedGridView/picture11.png

    In RowCreated EventHandler, we will give LinqDataSource CategoryID and Bind Gridview to Linq DataSource.

    JavaScript
    protected void Categories_RowCreated(object sender, GridViewRowEventArgs e)
       {
           var db = new NorthWindDataContext();
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               GridView GridView2 = (GridView)e.Row.Cells[1].FindControl("Products");
    
               var cat = db.Categories.Single(c => c.CategoryName ==
                         Categories.DataKeys[e.Row.DataItemIndex].Value.ToString());
    
    
               ProductsLinq.WhereParameters["CategoryID"].DefaultValue =
                                              cat.CategoryID.ToString();
               GridView2.DataSource = ProductsLinq;
           }
       }
    

Let's Discuss the Previous Code

JavaScript
GridView GridView2 = (GridView)e.Row.Cells[1].FindControl("Products");

Here, we make a reference to inner Gridview to deal with it, it is in the second column.

JavaScript
var cat = db.Categories.Single(c => c.CategoryName == Categories.DataKeys
          [e.Row.DataItemIndex].Value.ToString());

Here, we get Category Object of the CategoryName in the first Column using LinqQuery.

JavaScript
e.Row.DataItemIndex

This means Current Row, if your first Row is created so it takes first Category to get its ID.

JavaScript
ProductsLinq.WhereParameters["CategoryID"].DefaultValue = cat.CategoryID.ToString();

Here, we give CategoryID of Current Row to the LinqDataSource

JavaScript
GridView2.DataSource = ProductsLinq;

Bind GridView with LinqDataSource

History

  • 1st October, 2008: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Egypt Egypt
I am Student @ Misr University 4 Science & Technology , began programming at early age , when I was 14 ,I have worked with vb6 and java alot but now I am interesting in c# & .NET Technologies , My favourite Programming language is C++

Comments and Discussions

 
-- There are no messages in this forum --