HierarchicalDataGrid.aspx内容
<%@ Import namespace="System.Data"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>?ι??</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="FrmDataGrid" method="post" runat="server">
<P align="center">
<asp:DataGrid id="DataGrid1" BorderColor="#0099FF" runat="server" ShowHeader="False" Width="524px"
CellPadding="0" AutoGenerateColumns="False" BorderWidth="2px">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<FONT face=""></FONT>
</HeaderTemplate>
<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD bgColor="#ff6600"><B>订单号:
<%# DataBinder.Eval(Container.DataItem, "OrderID") %>
</B>
</TD>
</TR>
<TR>
<TD align="right">
<asp:DataGrid id=DataGrid2 runat="server" AutoGenerateColumns="False" BorderColor="#33FF33" DataKeyField="OrderID" DataSource='<%# ((DataRowView)Container.DataItem).CreateChildView("OrderRelation") %>'>
<HeaderStyle Font-Bold="True" ForeColor="#CC0066" BackColor="#FFCCFF"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="OrderID" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="产品名称">
<HeaderStyle Width="300px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="UnitPrice" HeaderText="单价"></asp:BoundColumn>
<asp:BoundColumn DataField="Quantity" HeaderText="数量"></asp:BoundColumn>
<asp:BoundColumn DataField="Discount" HeaderText="折扣"></asp:BoundColumn>
</Columns>
</asp:DataGrid></TD>
</TR>
</TABLE>
</ItemTemplate>
<EditItemTemplate>
<FONT face=""></FONT>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></P>
</form>
</body>
</HTML>
HierarchicalDataGrid.aspx.cs内容
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace HierarchicalDataGrid
{
public class HierarchicalDataGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1 = null;
#region " Web Form Designer Generated Code "
// 下面的调用对Web Form设计器来说是必须的
//[System.Diagnostics.DebuggerStepThrough()]
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
// CODEGEN: 这个方法是为Web Form设计器调用的
// 不要用代码编辑器改变这里.
// private void Page_Init(System.Object sender,System.EventArgs e)
// {
// InitializeComponent();
// }
#endregion
// 打开数据库连接
private void Page_Load(System.Object sender,System.EventArgs e)
{
SqlConnection OrdersConnection = new SqlConnection ("Server=localhost;Database=northwind;user id = sa; password=sa;");
// 得到订单和订单详细信息
SqlDataAdapter OrderAdapter = new SqlDataAdapter ("Select * from Orders; select OrderID ,Products.ProductName,[order Details].Unitprice,[order Details].Quantity,[order Details].discount from [order Details],Products where [Order Details].ProductId=Products.ProductID",OrdersConnection);
// 创建并填充DataSet对象
DataSet OrderDataSet = new DataSet ();
OrderAdapter.Fill(OrderDataSet);
// 设定表名字
OrderDataSet.Tables[0].TableName = "Orders";
OrderDataSet.Tables[1].TableName = "Order Details";
// 在基于OrderID字段的两个表之间建立父子关系
DataColumn Parent = OrderDataSet.Tables["Orders"].Columns["OrderID"];
DataColumn Child = OrderDataSet.Tables["Order Details"].Columns["OrderID"];
DataRelation OrderRelation = new DataRelation ("OrderRelation", Parent, Child, false);
// 为DataSet添加关系
OrderDataSet.Relations.Add(OrderRelation);
// 把DataGrid绑定到Orders表
DataGrid1.DataSource = OrderDataSet.Tables["Orders"].DefaultView;
DataBind();
}
private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
e.Item.Cells[0].BackColor = System.Drawing.Color.Ivory;
}
}
}