Thursday, July 17, 2014

Nested Grid Through ADO.NET

In aspx put the grid in the given way-:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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="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" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#594B9C" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#33276A" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code Behind File will be like this--:

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 _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbMandiConnectionString"].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;
                }
            }
        }
    }
}