write below code to the NetedGrid.aspx page
<asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px"
CellPadding="3" CellSpacing="1" GridLines="None"
onrowcommand="gvParent_RowCommand">
<Columns>
<asp:BoundField DataField="Grp_Id" HeaderText="ID" SortExpression="Grp_Id" />
<asp:BoundField DataField="GrpNm" HeaderText="Name" SortExpression="GrpNm" />
<asp:TemplateField HeaderText="Group">
<ItemTemplate>
<asp:Button ID="btnShowChild" runat="server"
CommandArgument='<%# Bind("Grp_Id") %>' CommandName="ShowChild" Text="+" />
<asp:Button ID="btnHideChild" runat="server"
CommandArgument='<%# Bind("Grp_Id") %>' CommandName="HideChild" Text="-" Visible="false" />
<asp:GridView ID="gvChild" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Acct_Id" HeaderText="AcctId"
SortExpression="Acct_Id" />
<asp:BoundField DataField="PartyNm" HeaderText="Party Name"
SortExpression="PartyNm" />
<asp:BoundField DataField="TempAdd" HeaderText="Address"
SortExpression="TempAdd" />
</Columns>
</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" />
</asp:GridView>
code for NestedGrid.aspx.cs file
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class NestedGrid : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["adv"].ConnectionString);
string query = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindParentGrid();
}
}
public void BindParentGrid()
{
query = "SELECT [Grp_Id] ,[GrpNm] FROM [dbo].[tbl_Group]";
SqlDataAdapter sdap = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
sdap.Fill(ds);
gvParent.DataSource = ds;
gvParent.DataBind();
ds.Clear();
}
protected void gvParent_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName== "ShowChild")
{
query = "SELECT * FROM [dbo].[tbl_AccountMaster] where [Grp_Id]="+e.CommandArgument.ToString()+"";
SqlDataAdapter sdap = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
sdap.Fill(ds);
for (int i = 0; i < gvParent.Rows.Count; i++)
{
if (e.CommandArgument.ToString() == (gvParent.Rows[i].Cells[0].Text))
{
((GridView)gvParent.Rows[i].FindControl("gvChild")).DataSource = ds;
((GridView)gvParent.Rows[i].FindControl("gvChild")).DataBind();
((Button)gvParent.Rows[i].FindControl("btnShowChild")).Visible = false;
((Button)gvParent.Rows[i].FindControl("btnHideChild")).Visible = true;
}
}
}
if (e.CommandName == "HideChild")
{
for (int i = 0; i < gvParent.Rows.Count; i++)
{
if (e.CommandArgument.ToString() == (gvParent.Rows[i].Cells[0].Text))
{
((GridView)gvParent.Rows[i].FindControl("gvChild")).DataSource = null;
((GridView)gvParent.Rows[i].FindControl("gvChild")).DataBind();
((Button)gvParent.Rows[i].FindControl("btnShowChild")).Visible = true;
((Button)gvParent.Rows[i].FindControl("btnHideChild")).Visible = false;
}
}
}
}
}
No comments:
Post a Comment