Click here to Skip to main content
15,892,005 members
Articles / Web Development / ASP.NET

Multilevel Nested Master/Detail Data Display Using GridView

Rate me:
Please Sign up or sign in to vote.
4.36/5 (46 votes)
18 Jul 20066 min read 519.3K   8.5K   162  
How GridViews can be used to show data having multilevel master detail relationships.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ParentGridView_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        int parent_index = e.NewEditIndex;
        
        //to set the edit index of the Parent grid with that of the current row
        ParentGridView.EditIndex = parent_index;
        ParentGridView.DataBind();
        //find the pubid_lbl containing pub_id in that particular row by using findcontrol method
        GridViewRow row = ParentGridView.Rows[parent_index];
        Label pub_id_lbl = (Label)row.FindControl("pubid_lbl");
       
        //save pub_id and edit_index in session for childgridview's use
        Session["PubID"] = Convert.ToInt32(pub_id_lbl.Text);
        Session["ParentGridViewIndex"] = parent_index;

   }
    protected void ChildGridView_OnRowEditing(object sender, GridViewEditEventArgs e)
    {

        //set the edit index of the child gridview with that of the current row
        int parent_index =(int)Session["ParentGridViewIndex"];
        GridViewRow parent_row = ParentGridView.Rows[parent_index];
        GridView ChildGridiView = (GridView)parent_row.FindControl("ChildGridView");

        int child_index = e.NewEditIndex;
        ChildGridiView.EditIndex = child_index;
        ChildGridiView.DataBind();
        //find the titleid_lbl in that particular row by using findcontrol method
        GridViewRow child_row = ChildGridiView.Rows[child_index];
        Label titleid_lbl = (Label)child_row.FindControl("titleid_lbl");
        // save the title_id in session for grandchildgridview's use
        Session["TitleID"] = titleid_lbl.Text;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Pakistan Pakistan
Mohyuddin is a Senior Software Engineer with BS Honors degree in Computer Science from Punjab University College of Information Technology (PUCIT),Lahore Pakistan. His interest and expertise are in Microsoft technologies specially SharePoint 2010, InfoPath 2010 , Workflow , MS Dynamics CRM, ASP.NET 2.0/3.0, ASP.NET MVC, C#,ADO.NET, LINQ , SQl Server ,Ajax, JQuery. Currently he is working at VeriPark Gulf, Dubai, UAE. He can be reached at gmohyd at g mail.com.

Comments and Discussions