Appear in Online Interview to test your skills and be ready for job!
Twitter Twitter LinkedIn YouTube Google
Be Interview ready ! | Search
Make friends on DotNetFunda.com and expand your network!
Winners

Win Prizes

Announcements
Like us on Facebook
Sponsor
Top Articles Author
Fri, 25-Apr-2014 Authors
All Time Authors
Sourav.Kayal
39750
Sheonarayan
34800
Niladri.Biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads

How to work with the nested GridView (a GridView inside another GridView) and populate the data?

SheoNarayan
Posted by under ASP.NET category on for Intermediate level | Points: 250 | Views : 13680 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
Rating: 5 out of 5
1 vote(s)


To work with nest GridView (a GridView inside another GridView), we can follow this approach.

Get hundreds of ASP.NET Tips and Tricks and ASP.NET Online training here.

In the previous article we learnt about How to do custom pagination in the GridView to achieve better performance in case we have large number of data to display? In this article we shall learn how to  work with nest GridView (a GridView inside another GridView), we can follow this approach.

ASPX PAGE

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" BorderStyle="Solid" BorderWidth="1"

BorderColor="Brown" RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1"

RowStyle-BorderColor="ControlDarkDark" OnRowDataBound="FindTheGridAndBound"

DataKeyNames="AutoId" CellPadding="4" CellSpacing="2"

BackColor="Azure">

<Columns>

<asp:TemplateField HeaderText="Personal Details">

<ItemTemplate>

<%# Eval("AutoId") %> - <%# Eval("FirstName") + " " +

Eval("LastName") %> files :

<asp:GridView ID="GridViewNested" runat="server"

EnableViewState="false" BackColor="Aquamarine" Width="100%" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

 

In the above code snippet, on the .aspx page we have a GridView inside a GridView (it is also called nested GridView, in the same way we can have nested ListView, nested Repeater control etc.). In the page GridView we have specified OnRowDataBound event that fires when each row is bounded to the data. On OnRowDataBound event we have attached “FindTheGridAndBound” that executes when each row is bounded to the GridView.


CODE BEHIND

string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;


protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

GetData();

}

}


private void GetData()

{

DataTable table = new DataTable();

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))

{

// write the sql statement to execute

string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail ORDER By AutoId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))

{

// fire Fill method to fetch the data and fill into DataTable

ad.Fill(table);

}

}

}

// specify the data source for the GridView

GridView1.DataSource = table;

// bind the data now

GridView1.DataBind();

}

 

protected void FindTheGridAndBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType != DataControlRowType.DataRow) return;

GridView nestedGridView = (GridView)e.Row.FindControl("GridViewNested");

int personalDetailsId =

int.Parse(GridView1.DataKeys[e.Row.RowIndex].Value.ToString());

DataTable table = new DataTable();

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))

{

// write the sql statement to execute

string sql = "SELECT AutoId, FileName, Active FROM Files WHERE PersonalDetailsId = @personalDetailsId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))

{

cmd.Parameters.AddWithValue("@personalDetailsId", personalDetailsId);

// fire Fill method to fetch the data and fill into DataTable

ad.Fill(table);

}

}

}

nestedGridView.DataSource = table;

nestedGridView.DataBind();

}

 

FindTheGridAndBound() method

In this method, we have first checked for the RowType, if it is of DataRow type then only we are proceeding further else we are exiting from this method (if we will not check this condition, the whole code of this method will execute in case of header, footer and other types of rows as well and the code might throw error).

Now we have found the nested GridView using FindControl method and then retrieved the AutoId (primary key of my parent GridView). Using ADO.NET we have retrieved the files related with this AutoID (here called PersonalDetailsId) and populated into the nested GridView.

NOTE: Similary you can nest other Data controls such as DataList, Repeater, ListView and even DropDownList control as well.

OUTPUT

Thanks for reading, hope you liked it.

Keep reading my forth coming articles. To read my series of articles on ASP.NET,click here.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:




About the Author

SheoNarayan

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Microsoft_MVP,Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan The Founder http://www.dotnetfunda.com
http://www.snarayan.com

Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan
Login to vote for this post.

Comments or Responses

Posted by: Sabarimahesh on: 3/16/2012 | Points: 25

Sheo Narayan

Thank You


Login to post response.

Comments / Responses
Select text & click toolbar to format. Formatting appears in viewmode only. HTML Tags are not allowed.
Bold Italic Underline Paragraph Title Code  Link 
 Wait ... Processing ..... please wait.

Comment using Facebook