Page views:
123480

In this article I’m going to explain how to create 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. Here I’ll show you how to create Nested GridView in ASP.NET. First we have to create two 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

float

month

varchar(10)

year

varchar(10)

creditedon

varchar(10)

 

Create table script:

CREATE TABLE [dbo].[Salary_Details](

        [empid] [varchar](50) NULL,

        [salary] [float] NULL,

        [month] [varchar](10) NULL,

        [year] [varchar](10) NULL,

        [creditedon] [varchar](10) NOT NULL

) ON [PRIMARY]

 Expand Collapse:

          In this demo I’ve used JavaScript function for expand collapse functionality to the GridView. Sample code is given below

    <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";

            }

        }

    </script>

          

 Designer Source Code:

<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";

            }

        }

    </script>

</head>

<body>

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

<div>

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

                                                    ShowFooter="true"

        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">

                    <Columns>

                    <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"/>

                    </Columns>

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

                    </asp:GridView> 

                </td>

            </tr>

        </ItemTemplate>

        </asp:TemplateField> 

    </Columns>

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

</asp:GridView>    

</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";       

        SqlDataAdapter adp = new SqlDataAdapter(cmdstr,conn);

        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();           

    }

    }

}

 

 

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/1011/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