ASP.NET tips: Display resultset from Multiple DataTable

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.

Shahed Khan
CEO Simplexhub
Co-Founder PageFlakes
Founder Smart Code Generator

Leave a Reply

Your email address will not be published. Required fields are marked *