100% found this document useful (2 votes)
14K views

Nested GridView With ASP - Net C#

I created one document on the nested gridview which shows how to implement nested girdview in asp.net with C#. This document contains Screen shot and Code

Uploaded by

Kashyap Patel
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
14K views

Nested GridView With ASP - Net C#

I created one document on the nested gridview which shows how to implement nested girdview in asp.net with C#. This document contains Screen shot and Code

Uploaded by

Kashyap Patel
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy