Click here to Skip to main content
15,911,896 members
Articles / Web Development / ASP.NET
Article

Embedding DataGrid inside a DataGrid

Rate me:
Please Sign up or sign in to vote.
1.15/5 (19 votes)
16 Aug 2004 101.6K   33   14
Embedding DataGrid inside a DataGrid.

Introduction

In this code sample, I will explain how you can embed one DataGrid inside another DataGrid. Here is what embedding DataGrid inside a DataGrid looks like:

Sample screenshot

Purpose

That's a good question. why does one want a DataGrid inside a DataGrid. Well, answer is simple, to view information in groups. Suppose if you have one user who has bought several items, you can view details by using child DataGrid inside main DataGrid.

NOTE

Please don't use open queries while accessing database. Always use stored procedures.

.cs code

C#
private void Page_Load(object sender, System.EventArgs e)
{
  if(!Page.IsPostBack) 
  { 
    BindData(); 

  }
}

public void BindData() 
{ 
  SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories",myConnection); 

  DataSet ds = new DataSet(); 
  ad.Fill(ds,"Categories"); 

  OuterGrid.DataSource = ds; 
  OuterGrid.DataBind(); 

}

protected DataView GetDataSource(Int32 catID) 
{ 
  string query = "SELECT * FROM Categories"; 
  SqlDataAdapter ad = new SqlDataAdapter(query,myConnection); 
  DataSet ds = new DataSet(); 
  ad.Fill(ds,"Categories"); 

  DataView _orders = ds.Tables["Categories"].DefaultView;
  _orders.RowFilter = "CategoryID='" + catID + "'";
  return _orders;

}

HTML sourcecode

OK. I am not able to paste the HTML code here. Something wrong with this editor. So I am going to tell you how to do it. It's simple. In the DataGrid HTML code, just add a DataSource tag and make the source equals to GetDataSource method as it is described earlier.

And that's it! I will surely try to update this article since the HTML code is missing!

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


Written By
Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.




I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions

 
GeneralMy vote of 1 Pin
bholashankar18-Jan-10 19:54
bholashankar18-Jan-10 19:54 
GeneralSee better tutorial Pin
Michael Freidgeim17-Aug-06 13:51
Michael Freidgeim17-Aug-06 13:51 
GeneralGetting Error Pin
Rajesh_817-Aug-06 21:15
Rajesh_817-Aug-06 21:15 
GeneralDataList Within A DataGrid Pin
mshariq12-Jul-06 3:50
mshariq12-Jul-06 3:50 
GeneralRe: DataList Within A DataGrid Pin
Siva Myneni5-Feb-07 0:00
Siva Myneni5-Feb-07 0:00 
GeneralRe: DataList Within A DataGrid Pin
Siva Myneni5-Feb-07 0:02
Siva Myneni5-Feb-07 0:02 
GeneralRe: DataList Within A DataGrid Pin
mshariq14-Feb-07 0:07
mshariq14-Feb-07 0:07 
GeneralRe: DataList Within A DataGrid Pin
Siva Myneni14-Feb-07 17:02
Siva Myneni14-Feb-07 17:02 
GeneralRe: DataList Within A DataGrid Pin
mshariq14-Feb-07 18:32
mshariq14-Feb-07 18:32 
Basically now i m trying to keep the datalist within a datalist using northwind employees table. i want that the country list should b displayed horizontally & emp name should b displayed vertically.i m receiving an exception. "Object reference not set to an instance of an object." when the debugger comes 2
EmpList = e.Item.FindControl("CountryList")
If it is possible 4 u, plz sort this prob. I m also trying 2 send u the whole. u can check it urself as well.

GeneralHey Pin
Andre da Silva Carrilho8-Mar-06 6:30
Andre da Silva Carrilho8-Mar-06 6:30 
GeneralHumm Pin
Anonymous16-Aug-04 20:26
Anonymous16-Aug-04 20:26 
GeneralRe: Humm Pin
azamsharp17-Aug-04 4:28
azamsharp17-Aug-04 4:28 
GeneralRe: Humm Pin
Anonymous8-Aug-05 9:05
Anonymous8-Aug-05 9:05 
GeneralRe: Humm Pin
Labrat00217-Aug-04 15:23
Labrat00217-Aug-04 15:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.