(1). Display
(2)Code
1. Front-End Interface code:
<% @ Page Language = "C #" codebehind = "webform1.aspx. CS "autoeventwireup =" false "inherits =" The data binding control displays both the master table and sub-table data. webform1 "%>
<% @ Import namespace = "system. Data" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform1 </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<! -- Bind primary table data -->
<Asp: repeater id = "myrepeater" runat = "server">
<Headertemplate>
<Table border = "0" bgcolor = "lightblue">
</Headertemplate>
<Itemtemplate>
<Tr>
<TD style = "background-color: lightgray">
<B>
<% # Databinder. eval (container. dataitem, "typeid") %>
& Nbsp;
<% # Databinder. eval (container. dataitem, "typename") %>
</B>
<Br>
<! -- Bind sub-table data -->
<Asp: repeater id = "childrepeater" datasource = '<% # (datarowview) container. dataitem)
. Row. getchildrows ("typerelation") %> 'runat = "server">
<Itemtemplate>
<Table border = "0" bgcolor = "# ffcc33">
<Tr>
<TD>
& Nbsp;
<% # Databinder. eval (container. dataitem, "[/" typeid/"]") %>
& Nbsp;
<% # Databinder. eval (container. dataitem, "[/" typedetail/"]") %>
<Br>
</TD>
</Tr>
</Table>
</Itemtemplate>
</ASP: repeater>
</TD>
</Tr>
</Itemtemplate>
<Footertemplate>
</Table>
</Footertemplate>
</ASP: repeater>
</Form>
</Body>
</Html>
2. Future code
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
The namespace data binding control displays both the master table and sub-table data.
{
/// <Summary>
/// The data binding control displays both the master table and sub-table data.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Repeater myrepeater;
Private void page_load (Object sender, system. eventargs E)
{
Dataset DS = new dataset ();
// 1. Create a master table
Datatable dttypeparent = new datatable ();
Dttypeparent. Columns. Add (New datacolumn ("typeid", typeof (INT )));
Dttypeparent. Columns. Add (New datacolumn ("typename", typeof (string )));
// Add two records to the master table
Datarow drparent1 = dttypeparent. newrow ();
Drparent1 ["typeid"] = 1;
Drparent1 ["typename"] = "Fruit ";
Dttypeparent. Rows. Add (drparent1 );
Datarow drparent2 = dttypeparent. newrow ();
Drparent2 ["typeid"] = 2;
Drparent2 ["typename"] = "Toys ";
Dttypeparent. Rows. Add (drparent2 );
Dttypeparent. tablename = "typeparent ";
Dttypeparent. primarykey = new datacolumn [] {dttypeparent. Columns ["typeid"]};
DS. Tables. Add (dttypeparent );
// 2. Create a sub-table
Datatable dttypechild = new datatable ();
Dttypechild. Columns. Add (New datacolumn ("typeid", typeof (INT )));
Dttypechild. Columns. Add (New datacolumn ("typedetail", typeof (string )));
// Add five records to the sub-table
Datarow drchild1 = dttypechild. newrow ();
Drchild1 ["typeid"] = 1;
Drchild1 ["typedetail"] = "apple ";
Dttypechild. Rows. Add (drchild1 );
Datarow drchild2 = dttypechild. newrow ();
Drchild2 ["typeid"] = 1;
Drchild2 ["typedetail"] = "orange ";
Dttypechild. Rows. Add (drchild2 );
Datarow drchild3 = dttypechild. newrow ();
Drchild3 ["typeid"] = 1;
Drchild3 ["typedetail"] = "banana ";
Dttypechild. Rows. Add (drchild3 );
Datarow drchild4 = dttypechild. newrow ();
Drchild4 ["typeid"] = 2;
Drchild4 ["typedetail"] = "robot ";
Dttypechild. Rows. Add (drchild4 );
Datarow drchild5 = dttypechild. newrow ();
Drchild5 ["typeid"] = 2;
Drchild5 ["typedetail"] = "car ";
Dttypechild. Rows. Add (drchild5 );
Dttypechild. tablename = "typechild ";
DS. Tables. Add (dttypechild );
DS. relations. Add ("typerelation ",
DS. Tables ["typeparent"]. Columns ["typeid"],
DS. Tables ["typechild"]. Columns ["typeid"]);
Myrepeater. datasource = Ds. Tables ["typeparent"];
Page. databind ();
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}
(3) Sample download
Http://www.cnblogs.com/Files/ChengKing/DataRalationDisplay.rar