Geeks With Blogs

News Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009

I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta an ASP.NET MVC application written in C#.NET.

Some of My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Shahed Khan blog

I normally do not use DataSet and prefer Objects instead generated by the ORM frameworks, but recently I had to produce a ASP.NET page that displayed a list of records from multiple DataTables. Lets look at a similar example.

Lets assume we consume this DataSet which has two DataTables "Agent" and "RealEstateProperty". The task is to display, what properties belongs to which agent, in a GridView.


There are different ways to accomplish this task, lets look at them one by one.

Method 1: Bind DataTables to  a single GridView

To bind these multiple DataTables to a single GridView control, we can quickly create a new temporary DataTable, with the required fields and populate the Rows and then bind the new DataTable Rows to the GridView.


The above code is self explanatory, where I have created a temporary DataTable, populated its rows by iterating through the original DataTables and then binded the Grid to the new DataTable.Rows. The interesting piece of code to note here is the row.GetChildRows(), which respects the relationship and automatically returns the related child rows. The aspx part looks like the following, where we have a GridView with three columns.


and the RowDataBound code is


Method 2: Bind DataTables to a nested GridView

If we want to avoid creating temporary dummy DataTable as described in method 1, we can use the technique of nested list control. Now we can do this by using

two GridView, or
two DataList, or
two Repeaters or
a combination of Repeater and GridView, or
a combination of Repeater and DataList, or
a combination of GridView and DataList.

We are going to look at a combination of 2 GridViews here, where one GridView is nested inside another, you can do any of the above combinations. For this case the the aspx code is,nestedgridview

Notice the parent and nested GridViews has different onrowdatabound methods. In this technique the parent grid is binded to the the Agent DataTable Rows and the nested GridView is binded to the child RealEstateProperty DataTable Rows. Here is the code.

Method 3: Convert the DataSet to Objects and then bind to GridView

We can generate csharp class from the dataset schema using Xsd.exe and then bind the GridViews to the objects. I have discussed similar technique in one of my previous blog post, where you will find how we can use the Xsd.exe that ships with the .NET Framework.

    C:\temp>xsd propertyDataSet.xsd /l:cs /c
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 2.0.50727.42]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'C:\temp\propertydatasetclass.cs'.

I also demonstrated a handy DataTableToT() method to assist in converting DataTable to strongly typed object. And when we have the DataTable converted to a stronglytyped object/list/collection it is very easy to bind to the bind to the GridView.


Other Tips
1. DataSet also comes with handy Merge Method, check here, we could have also used this method.
2. When we load a DataSet from xml schema if we do not provide the strongly typed DataSet it will not preserve the Relationship automatically.


Note: The Relations Count = 0;

3. To preserve Relations automatically during a DataSet load from xml, provide strongly typed DataSet. Both of the following methods will automatically preserve the parent child relationship. Also even if you consume them from a webservice client application they will preserve the Relations.


4. DataSet.WriteXml() method serializes the DataSet content and writes XML data.
5. DataSet.WriteXmlSchema() method writes the DataSet structure as XML Schema (xsd file).


We have discussed different techniques to bind multiple DataTables to GridView, we have also discussed some handy tips about the DataSet. Hope this helps.

Posted on Wednesday, February 11, 2009 5:06 AM | Back to top

Comments on this post: ASP.NET tips: Display resultset from Multiple DataTable

# re: ASP.NET tips: Display resultset from Multiple DataTable
Requesting Gravatar...
Thanks admin nice nice post
su pompası
kilo verme
Left by mankenler on Sep 03, 2009 10:02 AM

# re: ASP.NET tips: Display resultset from Multiple DataTable
Requesting Gravatar...
Thanks admin nice post
Left by sesli sohbet on Sep 03, 2009 10:05 AM

# re: ASP.NET tips: Display resultset from Multiple DataTable
Requesting Gravatar...
yok şimdi ayak yapma admin
barbie oyunu
hot car picture
Left by tekne on Sep 03, 2009 10:06 AM

# re: ASP.NET tips: Display resultset from Multiple DataTable
Requesting Gravatar...
thanxs very nice post :)
Left by مسلسل شر النفوس 2 on Sep 13, 2009 5:04 AM

# re: ASP.NET tips: Display resultset from Multiple DataTable
Requesting Gravatar...
Thanks admin nice post
Left by دليل on Sep 17, 2009 6:26 AM

Your comment:
 (will show your gravatar)

Copyright © Shahed Khan | Powered by: