ASP.Net 2.0 Nested Gridview Using C#

by top54u.com 09 Mar, 2008
Spotlight.....

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 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.

 

HTML source code for nested Gridview:

 

<asp:GridView ID="grdV_categories" runat="server" AutoGenerateColumns="False" ShowHeader="False"
            DataKeyNames="categoryid">

<Columns>

<asp:TemplateField>
<ItemTemplate>

<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<b>Product list of <%# DataBinder.Eval (Container.DataItem, "CategoryName") %> :</b></td>
</tr>

<tr>
<td>

<asp:GridView ID="grdV_products" runat="server" AutoGenerateColumns="False" ShowHeader="False">

<Columns>

<asp:TemplateField>
<ItemTemplate>
<%# DataBinder.Eval (Container.DataItem, "ProductName") %>
</ItemTemplate>
</asp:TemplateField>

</Columns>

</asp:GridView>

 

</td>
</tr>
table>


ItemTemplate>
</asp:TemplateField>


</Columns>


</asp:GridView>

Understanding the asp.net nested gridview C# code

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 9:15:40 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....