Master Detail Using GridView and DetailsView

<%@ 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;



  1. private DataSet Data()

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

    here it generates can not find column..... Please provide more information over it. Provide links to related topics if possible.keep posting. Will be visiting back soon.

  2. So Simple, So Useful

    Thanks for giving the basic construction of this UI type. There are some wonderful and complex implementations on blogs out there but I needed this simple example to move forward in my project. Thanks