SIMPLE NESTED GRID IN ASP.NET

K011gusain89
Posted by K011gusain89 under C# category on | Points: 40 | Views : 3162
Source Code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript" >
function ol(divId) {
var ok = divId;
var ok2 = "DivID" + ok;
$("#"+ ok2).slideToggle();
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Gv1" runat="server" onrowdatabound="Gv1_RowDataBound" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField>
<ItemTemplate>

<asp:LinkButton ID="btnlink" runat="server" Text='<%#Eval("course_name")%>' OnClientClick='<%# "javascript:ol("+Eval("course_id")+");return false;" %>'></asp:LinkButton>

<div id='DivID<%#Eval("course_id")%>'>
<asp:GridView ID="Gv2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField><ItemTemplate><%#Eval("session")%></ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>





C# Code



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;
namespace Nested_Grid_View
{
public partial class Nested_Page : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"server=LOCAL-PC;database=DATABASE_NAME;integrated security=true");

protected void Page_Load(object sender, EventArgs e)
{

SqlCommand com = new SqlCommand("select * from tb_course",con);

con.Open();
DataTable dt = new DataTable();
dt.Load(com.ExecuteReader());

Gv1.DataSource = dt;
Gv1.DataBind();



}

protected void Gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{


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

GridView ok = e.Row.FindControl("Gv2") as GridView;


if (ok != null)
{
SqlCommand com = new SqlCommand("select * from tb_session",con);
con.Close();
con.Open();
ok.DataSource = com.ExecuteReader();
ok.DataBind();
con.Close();

}

else
{


}
}


}
}
}

Comments or Responses

Login to post response