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"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True"
               DataKeyNames="Id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
               <Columns>
                   <asp:BoundField DataField="Id" />
                   <asp:BoundField DataField="Name" />
               </Columns>
           </asp:GridView>
        
           <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px"
               Width="176px">
               <Fields>
                   <asp:BoundField DataField="Id" />
                   <asp:BoundField DataField="Qul" />
               </Fields>
           </asp:DetailsView>
       </div>
   </form>
</body>
</html>
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.DataBind();
            }
            else
            {
                GridView1.DataSource = Session["Parent"] as DataTable;
                GridView1.DataBind();

            }
        }
    }

    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();

        ds.Tables.Add(dt);
        ds.Tables.Add(dtc);
        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;
        DetailsView1.DataBind();
    }

}

2 comments:

  1. private DataSet Data()
    {

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



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

    ReplyDelete
  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

    ReplyDelete