Imports System.Data.SqlClient
Public Class HierarchicalDataGrid
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
'下面的调用对Web Form设计器来说是必须的
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 这个方法是为Web Form设计器调用的
'不要用代码编辑器改变这里.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'打开数据库连接
Dim OrdersConnection As New SqlConnection("Server=(local)\NetSDK;Database=northwind;Trusted_Connection=yes")
'得到订单和订单详细信息
Dim OrderAdapter As 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对象
Dim OrderDataSet As New DataSet()
OrderAdapter.Fill(OrderDataSet)
'设定表名字
OrderDataSet.Tables(0).TableName = "Orders"
OrderDataSet.Tables(1).TableName = "Order Details"
'在基于OrderID字段的两个表之间建立父子关系
Dim Parent As DataColumn = OrderDataSet.Tables("Orders").Columns("OrderID")
Dim Child As DataColumn = OrderDataSet.Tables("Order Details").Columns("OrderID")
Dim OrderRelation As DataRelation = New DataRelation("OrderRelation", Parent, Child, False)
'为DataSet添加关系
OrderDataSet.Relations.Add(OrderRelation)
'把DataGrid绑定到Orders表
DataGrid1.DataSource = OrderDataSet.Tables("Orders").DefaultView
DataBind()
End Sub
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
e.Item.Cells(0).BackColor = System.Drawing.Color.Ivory
End Sub
End Class
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="HierarchicalDataGrid.aspx.vb" Inherits="HierarchicalDataGrid.HierarchicalDataGrid" %>
<%@ 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="">
</HEAD>
<body>
<form id="FrmDataGrid" method="post" runat="server">
<P align="center">
<asp:DataGrid id="DataGrid1" BorderColor="#0099FF" runat="server" ShowHeader="False" Width="470px" CellPadding="0" CellSpacing="0" AutoGenerateColumns="False" BorderWidth="2px">
<Columns>
<asp:TemplateColumn>
<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='<%# Ctype(Container.DataItem,DataRowView).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>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></P>
</form>
</body>
</HTML>