ASPX Source File
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridChildGrid.aspx.cs" Inherits="GridChildGrid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False"
BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
CellSpacing="1" GridLines="None" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>' Font-Bold="True"
Font-Italic="False" Font-Names="Lucida Handwriting" Font-Size="Large" Font-Strikeout="False"
ForeColor="#6600CC"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Show">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Bind("id") %>'
CommandName="BtnView">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GridView">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
</div>
</form>
</body>
</html>
Design :

In Aspx.cs File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class GridChildGrid : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=TOPS21;Initial Catalog=MyExample;Integrated Security=True;Pooling=False");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Fill_Grid();
}
}
public void Fill_Grid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from tbl_user", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "BtnView")
{
int rowindex = Int32.Parse(e.CommandArgument.ToString());
SqlDataAdapter da = new SqlDataAdapter("select id,firstname,lastname,emailid,mobileno,city from tbl_user where id='"+rowindex+"'", con);
rowindex--;
DataSet ds = new DataSet();
da.Fill(ds);
((GridView)GridView1.Rows[rowindex].FindControl("GridView2")).DataSource = ds;
((GridView)GridView1.Rows[rowindex].FindControl("GridView2")).DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int id = int.Parse(((Label)GridView1.Rows[e.Row.RowIndex].FindControl("Label2")).ToString());
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label L = (Label)e.Row.FindControl("Label2");
int id = int.Parse(L.Text.ToString());
SqlDataAdapter da = new SqlDataAdapter("select emailid from tbl_user where id='" + id + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
((GridView)e.Row.FindControl("GridView2")).DataSource = ds;
((GridView)e.Row.FindControl("GridView2")).DataBind();
}
}
}
Output
On Button View of id = 2 Clicked.....
You will Get Below Snapshot as a output

If On Row Editing I have to Find Control Then
You Must Check The Second Condition
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int id = int.Parse(((Label)GridView1.Rows[e.Row.RowIndex].FindControl("Label2")).ToString());
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
Label L = (Label)e.Row.FindControl("Label2");
int id = int.Parse(L.Text.ToString());
SqlDataAdapter da = new SqlDataAdapter("select emailid from tbl_user where id='" + id + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
((GridView)e.Row.FindControl("GridView2")).DataSource = ds;
((GridView)e.Row.FindControl("GridView2")).DataBind();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridChildGrid.aspx.cs" Inherits="GridChildGrid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False"
BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
CellSpacing="1" GridLines="None" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>' Font-Bold="True"
Font-Italic="False" Font-Names="Lucida Handwriting" Font-Size="Large" Font-Strikeout="False"
ForeColor="#6600CC"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Show">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Bind("id") %>'
CommandName="BtnView">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GridView">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
</div>
</form>
</body>
</html>
Design :
In Aspx.cs File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class GridChildGrid : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=TOPS21;Initial Catalog=MyExample;Integrated Security=True;Pooling=False");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Fill_Grid();
}
}
public void Fill_Grid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from tbl_user", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "BtnView")
{
int rowindex = Int32.Parse(e.CommandArgument.ToString());
SqlDataAdapter da = new SqlDataAdapter("select id,firstname,lastname,emailid,mobileno,city from tbl_user where id='"+rowindex+"'", con);
rowindex--;
DataSet ds = new DataSet();
da.Fill(ds);
((GridView)GridView1.Rows[rowindex].FindControl("GridView2")).DataSource = ds;
((GridView)GridView1.Rows[rowindex].FindControl("GridView2")).DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int id = int.Parse(((Label)GridView1.Rows[e.Row.RowIndex].FindControl("Label2")).ToString());
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label L = (Label)e.Row.FindControl("Label2");
int id = int.Parse(L.Text.ToString());
SqlDataAdapter da = new SqlDataAdapter("select emailid from tbl_user where id='" + id + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
((GridView)e.Row.FindControl("GridView2")).DataSource = ds;
((GridView)e.Row.FindControl("GridView2")).DataBind();
}
}
}
Output
On Button View of id = 2 Clicked.....
You will Get Below Snapshot as a output
If On Row Editing I have to Find Control Then
You Must Check The Second Condition
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int id = int.Parse(((Label)GridView1.Rows[e.Row.RowIndex].FindControl("Label2")).ToString());
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
Label L = (Label)e.Row.FindControl("Label2");
int id = int.Parse(L.Text.ToString());
SqlDataAdapter da = new SqlDataAdapter("select emailid from tbl_user where id='" + id + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
((GridView)e.Row.FindControl("GridView2")).DataSource = ds;
((GridView)e.Row.FindControl("GridView2")).DataBind();
}
}
}
That's Really helpful
ReplyDeleteand it's verified by me.....