Default.aspx
<html>
<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>
<br />
<br />
<table width="600px" align="center">
<tr>
<td colspan="2" style="background-image:url(Images/header.jpg); height:70px">
<img src="Images/font1.png" alt="Dotnet-Fox" height="30px"/>
</td>
</tr>
<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>
<tr>
<td colspan="2" style="background-image:url(Images/header.jpg); height:30px"></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs
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=WINDOWSSERVER\\MSSQLSERVER2008;Database=test1;UID=epower;PWD=epower123");
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);
cmd.ExecuteNonQuery();
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);
cmd.ExecuteNonQuery();
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);
cmd.ExecuteNonQuery();
conn.Close();
gv_NestedChild.DataSource = ds;
gv_NestedChild.DataBind();
}
}
}
<html>
<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>
<br />
<br />
<table width="600px" align="center">
<tr>
<td colspan="2" style="background-image:url(Images/header.jpg); height:70px">
<img src="Images/font1.png" alt="Dotnet-Fox" height="30px"/>
</td>
</tr>
<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>
<tr>
<td colspan="2" style="background-image:url(Images/header.jpg); height:30px"></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs
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=WINDOWSSERVER\\MSSQLSERVER2008;Database=test1;UID=epower;PWD=epower123");
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);
cmd.ExecuteNonQuery();
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);
cmd.ExecuteNonQuery();
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);
cmd.ExecuteNonQuery();
conn.Close();
gv_NestedChild.DataSource = ds;
gv_NestedChild.DataBind();
}
}
}
No comments:
Post a Comment