Master Detail Using GridView and DetailsView in ASP.NET

What is GridView in ASP.NET?

Displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control enables you to select, sort, and edit these items.{alertInfo}

Creating a Master-Detail page is very challenging in ASP.NET. This post will show you how to create a master details page using GridView and DataList. Both are ASP.NET control for showing tabular data.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewInsideGridView.aspx.cs"
   Inherits="GridViewInsideGridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<head runat="server">
   <title>Untitled Page</title>
   <form id="form1" runat="server">
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True"
               DataKeyNames="Id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                   <asp:BoundField DataField="Id" />
                   <asp:BoundField DataField="Name" />
           <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px"
                   <asp:BoundField DataField="Id" />
                   <asp:BoundField DataField="Qul" />
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 GridViewInsideGridView : System.Web.UI.Page
    DataSet ds;
    protected void Page_Load(object sender, System.EventArgs e)
        ds = Data();
        if (!this.IsPostBack)
            if (Session["parent"] == null)
                GridView1.DataSource = ds.Tables["Parent"];
                GridView1.DataSource = Session["Parent"] as DataTable;


    private DataSet Data()

        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));

        dt.Rows.Add(new object[] { 1, "aaaa" });
        dt.Rows.Add(new object[] { 2, "bbbb" });
        dt.Rows.Add(new object[] { 3, "cccc" });
        dt.TableName = "Parent";

        DataTable dtc = new DataTable();
        dtc.Columns.Add("Id", typeof(int));
        dtc.Columns.Add("Qul", typeof(string));

        dtc.Rows.Add(new object[] { 1, "aaaa" });
        dtc.Rows.Add(new object[] { 2, "bbbb" });
        dtc.Rows.Add(new object[] { 3, "bbbb" });
        dtc.TableName = "Child";

        DataSet ds = new DataSet();

        Session["Parent"] = dt;

        return ds;

    protected void GridView1_SelectedIndexChanged(object sender, System.EventArgs e)

        DataView dv = new DataView(ds.Tables["Child"]);

        dv.RowFilter = "Id=" + this.GridView1.SelectedValue.ToString();
        DetailsView1.DataSource = dv;

Next Post Previous Post
No Comment
Add Comment
comment url