Tuesday, July 25, 2006


Asp.NET : Nested Gridview to show Master/Details relationship in ASP.NET 2.0

This will help you to nest grdiviews inside ASP.NET. There may be many ways to do this, but this is my way( May not be the best, let me know if you have any other ).

1. First drop a gridview in to the page, also set its DataKeyNames to your primary key

2. Add a new template column to this gridview.

3. Place another gridview inside this template column.

the code for the termplate column may look like this

<asp:TemplateField HeaderText=”Heading”>


<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox>



<asp:GridView ID=”GridView1″ runat=”server” DataSource=’<%# GetTrasnl(Convert.ToInt32(Eval(”pId”))) %>’ AutoGenerateColumns=”False” CellPadding=”4″ ForeColor=”Black” GridLines=”Vertical” BackColor=”White” BorderColor=”#DEDFDE” BorderStyle=”None” BorderWidth=”1px”>

<FooterStyle BackColor=”#CCCC99″ />


<asp:BoundField DataField=”language” HeaderText=”Language” />

<asp:BoundField DataField=”uName” HeaderText=”Translator” />


<RowStyle BackColor=”#F7F7DE” />

<SelectedRowStyle BackColor=”#CE5D5A” Font-Bold=”True” ForeColor=”White” />

<PagerStyle BackColor=”#F7F7DE” ForeColor=”Black” HorizontalAlign=”Right” />

<HeaderStyle BackColor=”#6B696B” Font-Bold=”True” ForeColor=”White” />

<AlternatingRowStyle BackColor=”White” />




This child gridview has to show the content based on the primary of the row thats binded to main gridview.

Here in my case “pId” is the primary key. The nesting happens here

<asp:GridView ID=”GridView1″ runat=”server” DataSource=’<%#GetTrasnl(Convert.ToInt32(Eval(”pId”))) %>’

what I have done is that, I have called function “GetTrasnl” and passed the primary key to that function. This function will return a datatable. So each time a row is bound to the main gridview, the child gridview is also bound with the corresponding values.

The functions is like this

Public Function GetTrasnl(ByVal prgId As Integer) As DataTable



End Function

Please post your comments if you have any

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?