ASP. NET binds multiple tables in DataSet.

Source: Internet
Author: User

This problem is rarely seen at ordinary times. Generally, data is stored in a table before data binding. In fact, if there is a relationship between tables, it is also possible to bind multiple tables. The following is an example of binding two tables in DataSet.

Copy codeThe Code is as follows: <% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
System. Data. DataSet ds = new System. Data. DataSet ();
System. Data. DataTable dataTable1 = new System. Data. DataTable ("BlogUser ");
System. Data. DataRow dr;
DataTable1.Columns. Add (new System. Data. DataColumn ("UserId", typeof (System. Int32 )));
DataTable1.Columns. Add (new System. Data. DataColumn ("UserName", typeof (System. String )));
DataTable1.PrimaryKey = new System. Data. DataColumn [] {dataTable1.Columns ["UserId"]};
For (int I = 0; I <8; I ++)
{
Dr = dataTable1.NewRow ();
Dr [0] = I;
Dr [1] = "Chapter E of Mencius" + I. ToString ();
DataTable1.Rows. Add (dr );
}
System. Data. DataTable dataTable2 = new System. Data. DataTable ("BlogArticle ");
DataTable2.Columns. Add (new System. Data. DataColumn ("ArticleId", typeof (System. Int32 )));
DataTable2.Columns. Add (new System. Data. DataColumn ("Title", typeof (System. String )));
DataTable2.Columns. Add (new System. Data. DataColumn ("UserId", typeof (System. Int32 )));
DataTable2.PrimaryKey = new System. Data. DataColumn [] {dataTable1.Columns ["ArticleId"]};
Random rd = new Random ();
For (int I = 0; I <20; I ++)
{
Dr = ableable2.newrow ();
Dr [0] = I;
Dr [1] = "article title" + I. ToString ();
Dr [2] = rd. Next (0, 7 );
DataTable2.Rows. Add (dr );
}
Ds. Tables. Add (dataTable1 );
Ds. Tables. Add (dataTable2 );
System. Data. DataColumn parentColumn = ds. Tables ["BlogUser"]. Columns ["UserId"];
System. Data. DataColumn childColumn = ds. Tables ["BlogArticle"]. Columns ["UserId"];
System. Data. DataRelation user_article = new System. Data. DataRelation ("UserId", parentColumn, childColumn );
Ds. Relations. Add (user_article );
This. Repeater1.DataSource = ds. Tables ["BlogArticle"]. DefaultView;
This. Repeater1.DataBind ();
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> multiple tables bound to DataSet by ASP. NET: Chapter E of Mencius </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: Repeater ID = "Repeater1" runat = "server">
<HeaderTemplate>
<Table border = "1">
<Tr style = "background: orange">
<Th> Article Title </th>
<Th> User ID </th>
<Th> User Name </th>
</Tr>
</HeaderTemplate>
<FooterTemplate>
</Table> </FooterTemplate>
<ItemTemplate>
<Tr>
<Td> <% # Eval ("Title") %> </td> <% # Eval ("UserId") %> </td>
<Td> <% # (Container. DataItem as System. Data. DataRowView). Row. GetParentRow ("UserId") ["UserName"] %>
</Td>
</Tr>
</ItemTemplate>
</Asp: Repeater>
</Form>
</Body>
</Html>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.