Multi Level DataGrid
This blog was originally posted at http://www.i-sources.net/?view=blog&blog_id=355647310
Emil Indradjaja, MCP
May 27th 2005
The following example is written to show how to multi-level DataGrid using ASP.NET 1.1.
The same concept can be applied in ASP.NET 2.0 using GridView.
The accomplish that I'd do the following steps:
1. Build a Parent Data Grid
The following code shwos a simple DataGrid that shows Region from Northwind Database
The code simply fills the MyDataSet with content from Region and Employees table and populates the DataSet into dgRegion during the 1st execution time of the page. The above code produces the following output:
For the sake of this article, we are going to add DataGrid for the employee (so called dgEmp) inside the item template of dgRegion as shown below:
2. Wire the parent and child event.
To make it work, we need to make few changes in the code behind of the page.
First, we need to wire to event for the parent datagrid as shown below:
After that, we need to wire the parent DataGrid for the ItemCreated event and ItemDataBound event
Inside the ItemCreated event of the parent datagrid, we find the dgEmp and add an ItemDataBound event to it. And, inside the ItemDataBound event of the parent datagrid, we find the the dgEmp and bind it to the dataview that corresponds to each employee.
3. Add event in the child datagrid.
The last thing to do is to add an event to the child datagrid as shown on the code below:
As you see, the function above just get the employee id and calculate the total sales from the ORDER table inside the Northwind database. The result of the code is shown below:
Code for this article is available here.