Click here to Skip to main content
13,551,196 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 17 Jun 2008
Licenced CPOL

Nest Gridviews using LinqDataSource

, 30 Sep 2008
Rate this:
Please Sign up or sign in to vote.
Here I will explain how to pur Gridview in other one , such as categories and products


Download - 7.97 KB


Hi dear Developers

I will talk here about creating 2 nested gridviews , using LinqDataSource and LinqToSql

, this can be done in many ways , but this is very simple way

let's see screen ShotScreenShot.png

Using the code

I will Explain it Step by Step

1. Create LinqToSql Class and put 2 tables Categories and Products

Picture1.png WebPage , make new GrdView and name it GridView1


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


Till Now the gridview will be like thatPicture5.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 Second Column , this 2nd Gridview will hold Products that are in category mentioned in first Column , So we will create Template Feild and put GridView in it


then add GridView InsideThis Field

5. Create AnotherLinq DataSource and Configure it as following


make DataSource take parameter CataegoryID , we will give the datasource CategoryID Programatically later


Here the LinqDataSource Will Select Products Accourding to CategoryID Parameter which is identified as WhereParameter


6. at this Point we don't do anything unless we put GridView In Second Column and Create another LinqDataSource which gets products by category ID , In next step we want to give LinqDataSource the CategoryID of the categoryName in 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


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 Previous Code

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

here we make reference to inner Gridview to deal with it , it is in 2nd 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 first Column using LinqQuery

e.Row.DataItemIndex : Means Current Row , if you first Row is Created so it take 1st 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


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


About the Author

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++

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 1 Oct 2008
Article Copyright 2008 by ms_soft89
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid