Nested Gridview(Inner Gridview)


Nested Gridview it is mainly used for all asp.net application.here i just added. 1.Step by Step process 2.Sql Script for table 3.Tested Source code both design view and code behind. 4.Screen Shot attached.

Inner Grid With Full Sample Code

Step 1:

Create two table 1.stud_table1 and stud_table2

stud_table1
-----------


USE [Test]
GO

/****** Object: Table [dbo].[Stud_Table1] Script Date: 10/11/2011 18:12:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Stud_Table1](
[uniqueid] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
[Department] [varchar](50) NOT NULL,
CONSTRAINT [PK_Stud_Table1] PRIMARY KEY CLUSTERED
(
[uniqueid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO



stud_table2
-----------

USE [Test]
GO

/****** Object: Table [dbo].[Stud_Table2] Script Date: 10/11/2011 18:13:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Stud_Table2](
[id] [int] IDENTITY(1,1) NOT NULL,
[uniqueid] [int] NOT NULL,
[Subject] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Stud_Table2] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Stud_Table2] WITH CHECK ADD CONSTRAINT [FK_Stud_Table2_Stud_Table1] FOREIGN KEY([uniqueid])
REFERENCES [dbo].[Stud_Table1] ([uniqueid])
GO

ALTER TABLE [dbo].[Stud_Table2] CHECK CONSTRAINT [FK_Stud_Table2_Stud_Table1]
GO

Step 2:

In Code Behind



SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["conn_str"].ConnectionString);

//-------------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindgrid1();

}
}



//-----------------------------------------------------------------------------------


void bindgrid1()
{

con.Open();
SqlCommand cmd = new SqlCommand("select * from stud_table1", con);

cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;

da.Fill(ds);
con.Close();
grd1.DataSource = ds;
grd1.DataBind();
}


//--------------------------------------------------------------------------------------



protected void gr1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataSet ds = new DataSet();

GridView grd2 = new GridView();
Label lbluniqueID = (Label)e.Row.FindControl("lbluniqueID");

grd2 = (GridView)e.Row.FindControl("grd2");
if (lbluniqueID != null)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from stud_table2 where uniqueid='" + Convert.ToInt32(lbluniqueID.Text) + "'", con);

cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;

da.Fill(ds);
con.Close();

if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{

grd2.DataSource = ds;
grd2.DataBind();
}

else
{
grd2.EmptyDataText = "No Data Found.";
grd2.DataBind();
}

}
}
}

//-------------------------------------------------------------------------------------------



That's it refer the attachment for screen print


Attachments

Comments

Guest Author: rajib02 May 2012

not clear!!!!!!!!!!!!



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: