Use two Repeater to output the format of associated data

Source: Internet
Author: User

For example, if there are two associated tables in the database, the following format must be implemented:

======================================
A
A1 A2 A3 A4 A5
B
B1 B2 B3 B4
C
C1 C2 C3 C4
......
......
======================================

For eagleapplication, use the following code.

SqlConnection Conn = new SqlConnection ("server =.; uid = sa; pwd = chengbo; database = northwind ;");
SqlDataAdapter daOrders = new SqlDataAdapter ("SELECT OrderID FROM Orders", Conn );
SqlDataAdapter daOrderDetails = new SqlDataAdapter ("SELECT OrderID, UnitPrice, Quantity FROM [Order Details]", Conn );
DataSet ds = new DataSet ();
DataTable dtOrders = ds. Tables. Add ("Orders ");
DataTable dtOrderDetails = ds. Tables. Add ("OrderDetails ");

Conn. Open ();
DaOrders. Fill (dtOrders );
DaOrderDetails. Fill (dtOrderDetails );
Conn. Close ();
Ds. Relations. Add ("OrdersAndDetails", dtOrders. Columns ["OrderID"], dtOrderDetails. Columns ["OrderID"]);

Foreach (DataRow drParent in dtOrders. Rows)
{
Console. WriteLine (drParent ["OrderID"]);
Foreach (DataRow drChild in drParent. GetChildRows ("OrdersAndDetails "))
{
Console. WriteLine ("{0} {1}/t", drChild ["UnitPrice"], drChild ["Quantity"]);
}
}

But what about web pages? Two Repeater implementations can be used.

. Aspx
<Asp: repeater id = "parentRepeater" runat = "server">
<Headertemplate>
<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0">
</Headertemplate>
<Itemtemplate>
<Tr>
<Td bgcolor = "#8c6979" height = "30" style = "PADDING-LEFT: 15px; FONT-SIZE: 12px; COLOR: # ffffff">
<% # DataBinder. Eval (Container. DataItem, "OrderID") %>
</Td>
</Tr>
<Tr>
<Td height = "1"> </td>
</Tr>
<Tr>
<Td bgcolor = "# c9a9b8" style = "PADDING: 5PX;">
<Asp: repeater id = "childRepeater" runat = "server" datasource = '<% # (DataRowView) Container. dataItem ). row. getChildRows ("OrdersAndDetails") %> '>
<Itemtemplate> [<% # DataBinder. eval (Container. dataItem, "[/" UnitPrice/"]") %>-<% # DataBinder. eval (Container. dataItem, "[/" Quantity/"]") %>] </itemtemplate>
</Asp: repeater> <br>
</Td>
</Tr>
<Tr>
<Td height = "1"> </td>
</Tr>
</Itemtemplate>
<Footertemplate>
</Table>
</Footertemplate>
</Asp: repeater>. aspx. cs
SqlConnection Conn = new SqlConnection ("server =.; uid = sa; pwd = chengbo; database = northwind ;");
SqlDataAdapter daOrders = new SqlDataAdapter ("SELECT OrderID FROM Orders", Conn );
SqlDataAdapter daOrderDetails = new SqlDataAdapter ("SELECT OrderID, UnitPrice, Quantity FROM [Order Details]", Conn );
DataSet ds = new DataSet ();
DataTable dtOrders = ds. Tables. Add ("Orders ");
DataTable dtOrderDetails = ds. Tables. Add ("OrderDetails ");

Conn. Open ();
DaOrders. Fill (dtOrders );
DaOrderDetails. Fill (dtOrderDetails );
Conn. Close ();
// Add Relation
Ds. Relations. Add ("OrdersAndDetails", dtOrders. Columns ["OrderID"], dtOrderDetails. Columns ["OrderID"]);

This. parentRepeater. DataSource = dtOrders;
// Call the DataBind method of the Page class and call the DataBind method of two Repeater object instances implicitly
This. Page. DataBind ();

Remember to add it to the top of the aspx File
<% @ Import Namespace = "System. Data" %>

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.