You are on page 1of 10

Nested Gridview Asp.net 2.

0 C#

Nested GridView with Asp.Net 2.0 C#

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

Default.aspx Design Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title>Nested GridView</title> </head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:GridView ID="grdParent" runat="server" AutoGenerateColumns="false"
OnRowCommand="grdParent_RowCommand">
<Columns>
<asp:BoundField HeaderText="State ID" DataField="StateID" />
<asp:BoundField HeaderText="State List" DataField="StateName" />
<%--This button is used to call the event of display the city list--%>
<asp:ButtonField ButtonType="button" CommandName="btnView" Text="List City" />
<%--This is the template field at where we can display result--%>
<asp:TemplateField>
<ItemTemplate>
<%--This is the inner girdview we bound city name here--%>
<asp:GridView ID="grdInner" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="CityName" HeaderText="City" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page


{
SqlConnection con = new SqlConnection(@"Data Source=SYSTE01\SQLEXPRESS;Initial Catalog=DemoDB;Integrated
Security=True;Pooling=False");
protected void Page_Load(object sender, EventArgs e)
{
//Bind State with parent gridview
try
{
con.Open();
SqlDataAdapter adpState = new SqlDataAdapter("SELECT * FROM State", con);
DataTable dtState = new DataTable();
adpState.Fill(dtState); // Fill data table with fetch records
con.Close();
grdParent.DataSource = dtState;
grdParent.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

}
protected void grdParent_RowCommand(object sender, GridViewCommandEventArgs e)
{

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

try
{
//Checking for command name which command/button is pressed
if (e.CommandName == "btnView")
{
//Find the which row buttom is pressed
int RowIndex = Convert.ToInt32(e.CommandArgument);

//Get State ID form the gridview


int RecordID = Convert.ToInt32(((GridView)e.CommandSource).Rows[RowIndex].Cells[0].Text);

con.Open();
SqlDataAdapter adpCity = new SqlDataAdapter("SELECT CityName FROM City where StateID=" + RecordID, con);

DataTable dtCity = new DataTable();


adpCity.Fill(dtCity);

//Bind Data with gridview with contorl found on that row

((GridView)grdParent.Rows[RowIndex].FindControl("grdInner")).DataSource = dtCity;
((GridView)grdParent.Rows[RowIndex].FindControl("grdInner")).DataBind();
con.Close();
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}

Kashyap Patel (kashyap_mca@hotmail.com)


Nested Gridview Asp.net 2.0 C#

Kashyap Patel (kashyap_mca@hotmail.com)

You might also like