Thursday, July 17, 2008

Nested GridView

Sometimes there will be tables in databases corresponding to another ones
Suppose we have a table named Customer and another named Orders(made by each customer)
In that case it will be better to use Nested GridView.
Here we are making it a little fancier using Javascript.

Here is a ScreenShot of the Nested GridView


1.Create a folder in your project called "Images" and put two images
arrowright.jpg and arrowdown.jpg which are arrows pointing to the defined directions.

2.Create a ConnectionString named "NorthWindConnectionString" in the web.config pointing to the Northwind DB in your system

In ASPX Page

<asp:GridView ID="GridView1" runat="server"

<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

<a href="javascript:ShowChildGrid('div<%# Eval("CustomerID") %>');">
<img id="imgdiv<%# Eval("CustomerID") %>" 
alt="Click to show/hide orders" 
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<td colspan="100%">
<div id="div<%# Eval("CustomerID") %>" style="display:none;position:relative;left:25px;" >
<asp:GridView ID="GridView2" runat="server"
EmptyDataText="No orders for this customer."
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<asp:BoundField DataField="OrderDate" HeaderText="Order Date" DataFormatString="{0:MMM-dd-yyyy}" HtmlEncode="False" />
<asp:BoundField DataField="ShippedDate" HeaderText="Shipped Date" DataFormatString="{0:MMM-dd-yyyy}" HtmlEncode="False" />
<asp:BoundField DataField="ShipCity" HeaderText="Shipped To" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Address], [City], [PostalCode], [Phone] FROM [Customers]">


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if(e.Row.RowType == DataControlRowType.DataRow)
GridView gv = (GridView)e.Row.FindControl("GridView2");
SqlDataSource dbSrc = new SqlDataSource();
dbSrc.ConnectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;
dbSrc.SelectCommand = "SELECT * FROM Orders WHERE CustomerID = '" + GridView1.DataKeys[e.Row.RowIndex].Value + "' ORDER BY OrderDate";
gv.DataSource = dbSrc;

Put this JavaScript in the header of your ASPX Page

function ShowChildGrid(obj)
var div = document.getElementById(obj);
var img = document.getElementById('img' + obj);
var theFlag = == "none"; = (theFlag) ? "inline" : "none";
img.src = (theFlag) ? "Images/arrowdown.jpg" : "Images/arrowright.jpg";

Happy Coding!

Related Posts :

7 comments on "Nested GridView"

Add your comment. Please don't spam!
Subscribe in a Reader
Anonymous said...

The blog is helpfull...
visit also [c#]

rajeswari on June 18, 2009 at 12:53 PM said...

Hi This is very useful. and also its easy to understand.

Santosh Kumar on July 10, 2009 at 4:08 PM said...

gud dude...itz really helped me a lot. Thnx n keep posting.

Anujesh on July 27, 2009 at 7:21 PM said...

Hi Naveen;
I am trying to build a Master/Detail type gridview structure.Your post was helpful.In the details section am implemeting Edit functionality.The event is firing but the field is not getting converted into text mode i.e i cannot make any changes.It's weird but I am lost.Can u help me over.

Il Presidente on September 14, 2009 at 1:55 PM said...

Thanks alot for this great article. I am however working on a multi nested gridview with 4 levels and cant get it to work. How would you write it?

Ryan J. on July 19, 2010 at 11:47 PM said...

I was about to approach this differently (read: more complicated) and ran into your post. Thank you for sharing! You saved me a good day of development.

Pravesh Singh on January 5, 2012 at 9:18 PM said...

Very informative post. It's really helpful for me and helped me lot to complete my task.

Thanks for sharing with us. I had found another nice post over the internet which was

also explained very well about GridView inside GridView in ASP.Net (Nested GridView), for

more details of this post check out this link...


Post a Comment