Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Nest Gridviews using LinqDataSource

0.00/5 (No votes)
30 Sep 2008 1  
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.

    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

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.

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.

e.Row.DataItemIndex

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

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

Here, we give CategoryID of Current Row to the LinqDataSource

GridView2.DataSource = ProductsLinq;

Bind GridView with LinqDataSource

History

  • 1st October, 2008: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here