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

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 23055 red flag
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

About the Author

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

Ex-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

Comment using Facebook(Author doesn't get notification)