利用ASP.NET DataGrid显示主次关系的数据
大家在实际工作中有没有遇到要在DataGrid中显示带有主次结构的数据呢?如果你对ADO.NET和SQL Server2000熟悉的话,我们下面就举一个利用VS.NET自带的示例数据库Northwind和VB.NET来说明如何实现这一目标。
首先,我们先准备要显示的带有主次结构的数据。
下面就是要绑定到HierarchicalDataGrid.aspx的VB.NET代码文件HierarchicalDataGrid.aspx.vb的内容:
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
在HierarchicalDataGrid.aspx中,我们为DataGrid1建立模板列,并把另外一个DataGrid嵌入到这个模板列,父一级的DataGrid用来显示主记录,比如:订单。子一级DataGrid用来显示详细信息,比如:订单的详细信息。我们这样为子DataGrid动态指定数据源:
DataSource='<%# Ctype(Container.DataItem,DataRowView).CreateChildView("OrderRelation") %>'>
上面这句就是实现显示主次关系数据的关键。根据当前父一级记录,DataRowView.CreateChildView利用前面创建的关系的名字作为参数获得子记录,并把它绑定到子DataGrid上。
下面就是我们要显示的ASPX页面的代码,HierarchicalDataGrid.aspx:
<%@ 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="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="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>
大家在实际工作中有没有遇到要在DataGrid中显示带有主次结构的数据呢?如果你对ADO.NET和SQL Server2000熟悉的话,我们下面就举一个利用VS.NET自带的示例数据库Northwind和VB.NET来说明如何实现这一目标。
首先,我们先准备要显示的带有主次结构的数据。
下面就是要绑定到HierarchicalDataGrid.aspx的VB.NET代码文件HierarchicalDataGrid.aspx.vb的内容:
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
在HierarchicalDataGrid.aspx中,我们为DataGrid1建立模板列,并把另外一个DataGrid嵌入到这个模板列,父一级的DataGrid用来显示主记录,比如:订单。子一级DataGrid用来显示详细信息,比如:订单的详细信息。我们这样为子DataGrid动态指定数据源:
DataSource='<%# Ctype(Container.DataItem,DataRowView).CreateChildView("OrderRelation") %>'>
上面这句就是实现显示主次关系数据的关键。根据当前父一级记录,DataRowView.CreateChildView利用前面创建的关系的名字作为参数获得子记录,并把它绑定到子DataGrid上。
下面就是我们要显示的ASPX页面的代码,HierarchicalDataGrid.aspx:
<%@ 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="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="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>