Tuesday, May 19, 2009

Nested Gridview Using C#

Gridview control in ASP.Net 2.0 can be nested in other Gridview control to display the related items of any item retrieved from relational SQL database using C# code. To work with nested Gridview control in ASP.Net 2.0 using C# you must be familiar with Type casting of control objects, Gridview DataKeyNames and DataKeys array collection of datakey values for each row of GridView and FindControl function.


GridView DataKeyNames:
DataKeyNames is an array collection of data key fields or you can say primary keys specified in the GridView control retrieved from the SQL database table that can used to uniquely identify the record. For example you want to edit any record using Gridview control then to pass the unique id to sql command to execute and edit the requested record, you can retrieve that id from the DataKeyNames collection of the GridView.




GridView DataKeys:
To get the value of data key field for any particular row of GridView control using C# code you can use DataKeys array by passing the rowIndex of GridView control to get the unique identity of that record.



GridView FindControl:
FindControl function is used to find any control by its server ID reference. Using FindControl function you can find the nested GridView control from the ItemTemplate cell of any row of ASP.Net GridView control.



To work with nested GridView Control you can use the Northwind database of SQL Server 2000. Consider the sql database tables categories and products to display the records. For example, you can display the category names as Title and related products in the nested GridView.

4 comments: