0% found this document useful (0 votes)
43 views

Gridview Inside Gridview

This document describes how to create a dataset with two related tables (Parent and Child) in C# code. It creates a dataset with two tables - Company (Parent) and Employees (Child). The Company table contains sample company records. The Employees table contains sample employee records with a foreign key to the Company table. A relation is defined between the two tables to link parent companies to child employees. The dataset is stored in session and returned to bind as the data source for nested GridViews to display the parent-child data relationship.

Uploaded by

api-26891935
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Gridview Inside Gridview

This document describes how to create a dataset with two related tables (Parent and Child) in C# code. It creates a dataset with two tables - Company (Parent) and Employees (Child). The Company table contains sample company records. The Employees table contains sample employee records with a foreign key to the Company table. A relation is defined between the two tables to link parent companies to child employees. The dataset is stored in session and returned to bind as the data source for nested GridViews to display the parent-child data relationship.

Uploaded by

api-26891935
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="DataRelation.aspx.cs"
Inherits="DataRelation" %>

<%@ Import Namespace="System.Data" %>


<!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">
<Columns>
<asp:BoundField DataField="CompanyName"
HeaderText="CompanyName" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2"
DataSource='<
%#((DataRowView)Container.DataItem).CreateChildView("ParentChild") %>'
runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Dept"
HeaderText="Department" />
<asp:BoundField DataField="name"
HeaderText="Name" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</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 DataRelation : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

GridView1.DataSource = CreateDS().Tables["Company"];
GridView1.DataBind();

}
private DataSet CreateDS()
{
DataSet ds;
if (Session["DataList_ParentChild"] == null)
{
ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName",
typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Dept", typeof(string)));
for (int i = 1; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company " + i;
dr[2] = "Address " + i;
dr[3] = "Manager name";
dr[4] = "Adminstration";
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
DataColumn[] Parent_PKColumns = new DataColumn[1];
Parent_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Parent_PKColumns;

dt = new DataTable("Employees");
dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyID", typeof(Int32)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Dept", typeof(string)));
for (int i = 1; i < 10; i++)
{
int imax = 0;
if (i % 2 == 0) imax = 5;
else imax = 4;
for (int y = 2; y < imax; y++) //3 emplyees for each
company
{
dr = dt.NewRow();
dr[0] = y + i * 5;
dr[1] = i;
dr[2] = "Employee # " + dr[0];
dr[3] = "Dept # " + (y + i);
dt.Rows.Add(dr);
}
}
DataColumn[] Child_PKColumns = new DataColumn[1];
Child_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Child_PKColumns;
ds.Tables.Add(dt);
DataColumn[] Child_FKColumns = new DataColumn[1];
Child_FKColumns[0] = dt.Columns["CompanyID"];

ds.Relations.Add("ParentChild", Parent_PKColumns,
Child_FKColumns);
Session["DataList_ParentChild"] = ds;
}
else
{
ds = (DataSet)Session["DataList_ParentChild"];
}
return ds;
}
}

You might also like