Below sample code is to show how a nested gridview can be added in a GridView Cell.
DB Design
Stuent Table
Student _id int
FirstName Varchar
LastName varchar
ContactID int
Contact table
ContactID int
Email varchar
HomePhone varchar
Mobile varchar
In a outer grid view all the data from table student will come and in column ContactId the data will be retrieved from the contact table and will be added in the inner Gridview .
Below is the code
NestedGridView .aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NestedGridView.aspx.cs" Inherits="NestedGridView" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="OuterGrid" runat="server"
onrowdatabound="OuterGrid_RowDataBound">
</asp:GridView>
</div>
</form>
</body>
</html>
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;
using System.Configuration;
using System.Web.Configuration;
public partial class NestedGridView : System.Web.UI.Page
{
// private string connstring = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ConnectionString;
private SqlConnection connObj;
private SqlDataAdapter objAdapter;
private DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Bind OuterGrid View Here
BindOuterGridView();
}
}
private void BindOuterGridView()
{
connObj = new SqlConnection();
connObj.ConnectionString = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ToString();
connObj.Open();
SqlCommand objCommand = new SqlCommand("Select Student_id, FirstName,LastName,Contactid from student ", connObj);
objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(ds);
connObj.Close();
OuterGrid.DataSource = ds.Tables[0];
OuterGrid.DataBind();
}
protected void OuterGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Create Inner GridView Here
DataSet ds = new DataSet();
GridView grd2 =new GridView ();
GridViewRow ROW = e.Row;
if (ROW.RowType != DataControlRowType.Header )
{
// grd2 = new GridView();
int contactID=0;
if(e.Row.Cells[3].Text!=" " )
contactID = int.Parse(e.Row.Cells[3].Text);
//grd2 = (GridView)e.Row.FindControl("grd2");
if (contactID != 0)
{
connObj.Open();
SqlCommand cmd = new SqlCommand("select * from Contact where Contactid=" + contactID, connObj);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
connObj.Close();
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
//Bind Inner GridView Here
grd2.DataSource = ds.Tables[0];
grd2.DataBind();
}
else
{
grd2.EmptyDataText = "No Data Found.";
grd2.DataBind();
}
}
//Add Inner GridView To Outer GridView's cell
e.Row.Cells[3].Controls.Add(grd2 );
}
}
}
}
DB Design
Stuent Table
Student _id int
FirstName Varchar
LastName varchar
ContactID int
Contact table
ContactID int
Email varchar
HomePhone varchar
Mobile varchar
In a outer grid view all the data from table student will come and in column ContactId the data will be retrieved from the contact table and will be added in the inner Gridview .
Below is the code
NestedGridView .aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NestedGridView.aspx.cs" Inherits="NestedGridView" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="OuterGrid" runat="server"
onrowdatabound="OuterGrid_RowDataBound">
</asp:GridView>
</div>
</form>
</body>
</html>
NestedGridView .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;
using System.Configuration;
using System.Web.Configuration;
public partial class NestedGridView : System.Web.UI.Page
{
// private string connstring = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ConnectionString;
private SqlConnection connObj;
private SqlDataAdapter objAdapter;
private DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Bind OuterGrid View Here
BindOuterGridView();
}
}
private void BindOuterGridView()
{
connObj = new SqlConnection();
connObj.ConnectionString = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ToString();
connObj.Open();
SqlCommand objCommand = new SqlCommand("Select Student_id, FirstName,LastName,Contactid from student ", connObj);
objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(ds);
connObj.Close();
OuterGrid.DataSource = ds.Tables[0];
OuterGrid.DataBind();
}
protected void OuterGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Create Inner GridView Here
DataSet ds = new DataSet();
GridView grd2 =new GridView ();
GridViewRow ROW = e.Row;
if (ROW.RowType != DataControlRowType.Header )
{
// grd2 = new GridView();
int contactID=0;
if(e.Row.Cells[3].Text!=" " )
contactID = int.Parse(e.Row.Cells[3].Text);
//grd2 = (GridView)e.Row.FindControl("grd2");
if (contactID != 0)
{
connObj.Open();
SqlCommand cmd = new SqlCommand("select * from Contact where Contactid=" + contactID, connObj);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
connObj.Close();
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
//Bind Inner GridView Here
grd2.DataSource = ds.Tables[0];
grd2.DataBind();
}
else
{
grd2.EmptyDataText = "No Data Found.";
grd2.DataBind();
}
}
//Add Inner GridView To Outer GridView's cell
e.Row.Cells[3].Controls.Add(grd2 );
}
}
}
}
Below is the ScreenShot attached of the o/p
good good good!!!
ReplyDelete