利用ASP.NET DataGrid显示主次关系的数据
2024-07-10 12:55:49
供稿:网友
大家在实际工作中有没有遇到要在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>