![]() |
Web Development »
ASP.NET »
Data
Intermediate
License: The Code Project Open License (CPOL)
Nest Gridviews using LinqDataSourceBy ms_soft89Here I will explain how to pur Gridview in other one , such as categories and products |
Javascript, CSS, HTML, XHTML, ASP, ASP.NET, Ajax
|
||||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
Download WebSite7.zip - 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 Shot
I will Explain it Step by Step
1. Create LinqToSql Class and put 2 tables Categories and Products

2.in 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 that
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
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Sep 2008 Editor: |
Copyright 2008 by ms_soft89 Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |