Nested Grid view
Desining .aspx
<head id="Head1" runat="server">
<title>Grid</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="jquery-1.3.2.min.js"
type="text/javascript"></script>
<script language="javascript"
type="text/javascript">
function divexpandcollapse(divname) {
var img = "img"
+ divname;
if ($("#"
+ img).attr("src") == "images/plus.gif") {
$("#" + img)
.closest("tr")
.after("<tr><td></td><td colspan =
'100%'>" + $("#" +
divname)
.html() +
"</td></tr>");
$("#" + img).attr("src", "images/minus.gif");
}
else {
$("#" + img).closest("tr").next().remove();
$("#" + img).attr("src", "images/plus.gif");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>Gridview Inside Gidview </legend>
<asp:GridView ID="grid" runat="server"
AutoGenerateColumns="false"
AllowPaging="true"
DataKeyNames="id"
OnPageIndexChanging="grid_PageIndexChanging"
OnRowDataBound="grid_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("id") %>');">
<img id="imgdiv<%#Eval("id") %>" width="9px" border="0" alt="plus" src="images/plus.gif"
/>
</a>
<div id="div<%# Eval("id") %>" style="display: none;">
<asp:GridView ID="grd" runat="server"
AutoGenerateColumns="false"
DataKeyNames="id">
<Columns>
<asp:BoundField DataField="E-id" HeaderText="Email" />
<asp:BoundField DataField="Mobile"
HeaderText="Mobile
No" />
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id"
HeaderText="Id"
HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-HorizontalAlign="Left" />
</Columns>
</asp:GridView>
</fieldset>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Gridemo :
System.Web.UI.Page
{
DataTable
dt = new DataTable();
Dal odal
= new Dal();
protected void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
fill();
}
}
public void fill()
{
string str = "select
id,name from registeruser";
dt =
SelectData(str);
grid.DataSource = dt;
grid.DataBind();
}
protected void
grid_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
grid.PageIndex = e.NewPageIndex;
fill();
}
private DataTable SelectData(string
sqlQuery)
{
SqlConnection con = new SqlConnection(@"Data Source=108.163.247.146;Initial
Catalog=elaunch_div;User ID=divuser;Password=div7*(");
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery, con))
{
DataTable dt = new
DataTable("registeruser");
sqlDataAdapter.Fill(dt);
return dt;
}
}
protected void
grid_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int id = int.Parse(grid.DataKeys[e.Row.RowIndex].Value.ToString());
GridView grd = (GridView)e.Row.FindControl("grd");
dt = SelectData("select * from
registeruser where id ='" + id + "'");
grd.DataSource = dt;
grd.DataBind();
}
}
}
Comments
Post a Comment