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

}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post