In this article I’m going to explain how to create multilevel nested GridView in ASP.NET using C#.

        The GridView control displays rows of data in a grid, displaying one data row per grid row.  This is what we’ve called Nested GridView. Multilevel or hierarchical GridView contains multiple GridView displayed in hierarchical order.

Note: If you are not aware of creating Nested GridView in ASP.NET you could read my previous article.

Nested GridView in ASP.NET using c#

          Here I’ll show you how to create Multilevel or hierarchical GridView. First we have to create tables,

Table Design (Parent grid):

Column Name

Data Type

empid

varchar(50)

name

varchar(100)

designation

varchar(100)

city

varchar(50)

country

varchar(50)

 

Create table script:

CREATE TABLE [dbo].[EmployeeDetails](

        [empid] [varchar](50) NULL,

        [name] [varchar](100) NULL,

        [designation] [varchar](100) NULL,

        [city] [varchar](50) NULL,

        [country] [varchar](50) NULL

) ON [PRIMARY]

 Table Design (Child grid):

Column Name

Data Type

empid

varchar(50)

salary_id

int

salary

float

month

varchar(10)

year

varchar(10)

creditedon

varchar(10)

 

Create table script:

CREATE TABLE [dbo].[Salary_Details](

        [empid] [varchar](50) NULL,

         [salary_id] [int] NULL,

        [salary] [float] NULL,

        [month] [varchar](10) NULL,

        [year] [varchar](10) NULL,

        [creditedon] [varchar](10) NOT NULL

) ON [PRIMARY]  

Table Design (Nested child grid):

Column Name

Data Type

salary_id

int

total_working_days

int

total_leave_taken

int

 

Create table script:

CREATE TABLE [dbo].[Leave_Details](

        [salary_id] [int] NULL,

        [total_working_days] [int] NULL,

        [total_leave_taken] [int] NULL

) ON [PRIMARY]

 

Designer Source Code: 

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

<!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>Nested Gridview in ASP.NET</title>

    <script language="javascript" type="text/javascript">

        function divexpandcollapse(divname) {

            var div = document.getElementById(divname);

            var img = document.getElementById('img' + divname);

            if (div.style.display == "none") {

                div.style.display = "inline";

                img.src = "Images/minus.gif";

            } else {

                div.style.display = "none";

                img.src = "Images/plus.gif";

            }

        } 

        function divexpandcollapseChild(divname) {

            var div1 = document.getElementById(divname);

            var img = document.getElementById('img' + divname);

            if (div1.style.display == "none") {

                div1.style.display = "inline";

                img.src = "../Images/minus.gif";

            } else {

                div1.style.display = "none";

                img.src = "../Images/plus.gif"; ;

            }

        }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <table width="600px" align="center">

    <tr>

    <td colspan="2">

    <asp:GridView ID="gvEmployeeDetails" runat="server"  AutoGenerateColumns="false" 

                        ShowFooter="true" Width="600px"

                        OnRowDataBound="gvEmployeeDetails_OnRowDataBound">

    <Columns>

        <asp:TemplateField ItemStyle-Width="20px">

            <ItemTemplate>

                <a href="JavaScript:divexpandcollapse('div<%# Eval("empid") %>');">

                    <img id="imgdiv<%# Eval("empid") %>" width="9px" border="0" 

                                                                 src="Images/plus.gif" alt="" /></a>                       

            </ItemTemplate>

            <ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>

        </asp:TemplateField>

        <asp:TemplateField HeaderText="Employee ID">

        <ItemTemplate>

            <asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,

                                                                    "empid") %>'></asp:Label>

        </ItemTemplate>

        </asp:TemplateField>               

        <asp:BoundField DataField="name" HeaderText="Name" />

        <asp:BoundField DataField="designation" HeaderText="Designation" />

        <asp:BoundField DataField="city" HeaderText="City" />

        <asp:BoundField DataField="country" HeaderText="Country" />

        <asp:TemplateField>

            <ItemTemplate>

            <tr>

            <td colspan="100%">

            <div id="div<%# Eval("empid") %>"  style="overflow:auto; display:none;

                                    position: relative; left: 15px; overflow: auto">

            <asp:GridView ID="gv_Child" runat="server" Width="95%"

                                    AutoGenerateColumns="false" DataKeyNames="empid"

                                    OnRowDataBound="gv_Child_OnRowDataBound">

            <Columns>

            <asp:TemplateField ItemStyle-Width="20px">

            <ItemTemplate>

            <a href="JavaScript:divexpandcollapse('div1<%# Eval("salary_id") %>');">

            <img id="imgdiv1<%# Eval("salary_id") %>" width="9px" border="0" src="Images/plus.gif"

                        alt="" /></a>                       

            </ItemTemplate>

            <ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>

            </asp:TemplateField>

            <asp:TemplateField HeaderText="Salary ID" Visible="false">

            <ItemTemplate>

                <asp:Label ID="lblSalaryID" runat="server" Text='<%#DataBinder.Eval

                                                            (Container.DataItem, "salary_id") %>'></asp:Label>

            </ItemTemplate>

            </asp:TemplateField>

            <asp:BoundField DataField="salary" HeaderText="Salary"/>

            <asp:BoundField DataField="month" HeaderText="Month"/>

            <asp:BoundField DataField="year" HeaderText="Year"/>

            <asp:BoundField DataField="creditedon" HeaderText="Credited On"/>

            <asp:TemplateField>

                <ItemTemplate>

                <tr>

                    <td colspan="100%">

                        <div id="div1<%# Eval("salary_id") %>"  style="overflow:auto; display:none;

                                                    position: relative; left: 15px; overflow: auto">

                        <asp:GridView ID="gv_NestedChild" runat="server" Width="95%"

                                                                AutoGenerateColumns="false">

                                        

                        <Columns>

                        <asp:BoundField DataField="total_working_days" HeaderText="Total working

                                                                                                                          days"/>

                        <asp:BoundField DataField="total_leave_taken" HeaderText="Total leave taken"/>

                        </Columns>

                        <HeaderStyle BackColor="#95B4CA" ForeColor="White" />

                        </asp:GridView>

                        </div>

                    </td>

                </tr>

                </ItemTemplate>

            </asp:TemplateField>

            </Columns>

            <HeaderStyle BackColor="#4D92C1" ForeColor="White" />

            </asp:GridView>

            </div>

            </td>

            </tr>

            </ItemTemplate>

        </asp:TemplateField>

    </Columns>

        <HeaderStyle BackColor="#0063A6" ForeColor="White" />

    </asp:GridView>    

    </td>

    </tr>       

        </table>

    </div>

    </form>

</body>

</html> 

 

Code Behind:

 

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

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

{

    SqlConnection conn = new SqlConnection("Data Source=SPIDER;Initial Catalog=Demo;

                                                         Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)

    {      

        if (!IsPostBack)

        {           

            BindGrid();

        }

    } 

    protected void BindGrid()

    {

        DataSet ds = new DataSet();       

        conn.Open();

        string cmdstr = "Select * from EmployeeDetails";

        SqlCommand cmd = new SqlCommand(cmdstr, conn);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);

        adp.Fill(ds);

        conn.Close();

        gvEmployeeDetails.DataSource = ds;

        gvEmployeeDetails.DataBind();

      

    }

    protected void gvEmployeeDetails_OnRowDataBound(object sender,  GridViewRowEventArgs e)   

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            Label lblEmpID = (Label)e.Row.FindControl("lblEmpID");         

            GridView gv_Child = (GridView)e.Row.FindControl("gv_Child"); 

            string txtempid = lblEmpID.Text; 

            DataSet ds = new DataSet();

            conn.Open();

            string cmdstr = "Select * from Salary_Details where empid=@empid";

            SqlCommand cmd = new SqlCommand(cmdstr, conn);

            cmd.Parameters.AddWithValue("@empid", txtempid);

            SqlDataAdapter adp = new SqlDataAdapter(cmd);

            adp.Fill(ds);

            conn.Close();

            gv_Child.DataSource = ds;

            gv_Child.DataBind();

           

        }

    } 

    protected void gv_Child_OnRowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            Label lblSalaryID = (Label)e.Row.FindControl("lblSalaryID");

            GridView gv_NestedChild = (GridView)e.Row.FindControl("gv_NestedChild"); 

            string txtempid = lblSalaryID.Text; 

            DataSet ds = new DataSet();

            conn.Open();

            string cmdstr = "Select * from Leave_Details where salary_id=@salary_id";

            SqlCommand cmd = new SqlCommand(cmdstr, conn);

            cmd.Parameters.AddWithValue("@salary_id", txtempid);

            SqlDataAdapter adp = new SqlDataAdapter(cmd);

            adp.Fill(ds);

            conn.Close();

            gv_NestedChild.DataSource = ds;

            gv_NestedChild.DataBind();

        }

    }

}

 

 

cytotec abortion

http://www.extrageek.com/template/default.aspx?an-abortion-pill buy abortion pills online
medical abortion pill online abortion pill abortion pill


View demo Download
http://www.dotnetfox.com/Document/1012/0Demo1.gif

If you enjoyed this article, get email updates (it's free).



Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox