Click here to Skip to main content
Click here to Skip to main content

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

Code

Download WebSite7.zip - 7.97 KB

Introduction

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

2.in WebPage , make new GrdView and name it GridView1

Picture2.png

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

Picture4.png

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

Picture6.png

then add GridView InsideThis Field

5. Create AnotherLinq DataSource and Configure it as following

Picture8.png

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

Picture13.png

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

Picture10.png

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

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

License

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

About the Author

ms_soft89
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 --
| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 1 Oct 2008
Article Copyright 2008 by ms_soft89
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid