在实际的应用中经常会遇到根据其它列计算某一新列的结果,实现这样的功能有两种办法:一个直接使用sql语句;另外就是在绑定时进行动态添加。第一种方法以前已经介绍过。下面就是第二种方法的具体实现:
adddatasetcolumn.aspx
<%@ page language="<a href="http://dev.21tx.com/language/vb/" target="_blank">vb</a>" autoeventwireup="false" codebehind="adddatasetcolumn.aspx.vb"
inherits="aspx<a href="http://dev.21tx.com/web/" target="_blank">web</a>.adddatasetcolumn"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>adddatasetcolumn</title>
<meta name="generator" content="microsoft visual studio <a href="http://dev.21tx.com/do.net/" target="_blank">.net</a> 7.1">
<meta name="code_language" content="visual basic .net 7.1">
<meta name="vs_defaultclientscript" content="<a href="http://dev.21tx.com/web/javascript/" target="_blank">javascript</a>">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="form1" method="post" runat="server">
<asp:datagrid id="datagrid1" runat="server" autogeneratecolumns="false"
showfooter="true" width="100%">
<headerstyle font-names="宋体" font-bold="true" horizontalalign="center"
forecolor="navy" backcolor="lightsalmon"></headerstyle>
<footerstyle backcolor="#ffccff"></footerstyle>
<columns>
<asp:templatecolumn headertext="订单号">
<itemtemplate>
<asp:label id="orderid" runat="server"></asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="产品号">
<itemtemplate>
<asp:label id="productid" runat="server"></asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="单价">
<itemtemplate>
<asp:label id="unitprice" runat="server"></asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="数量">
<itemtemplate>
<asp:label id="quantity" runat="server"></asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="折扣">
<itemtemplate>
<asp:label id="discount" runat="server"></asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="总计">
<itemtemplate>
<asp:label id="lbltotal" runat="server"></asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>
adddatasetcolumn.aspx.vb
imports system.web.ui.webcontrols
public class adddatasetcolumn
inherits system.web.ui.page
#region " web 窗体设计器生成的代码 "
'该调用是 web 窗体设计器所必需的。
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
end sub
protected withevents datagrid1 as system.web.ui.webcontrols.datagrid
'注意: 以下占位符声明是 web 窗体设计器所必需的。
'不要删除或移动它。
private designerplaceholderdeclaration as system.object
private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
'codegen: 此方法调用是 web 窗体设计器所必需的
'不要使用代码编辑器修改它。
initializecomponent()
end sub
#end region
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
dim strcnn as string = "data source=..netsdk; initial catalog=northwind;user id=sa;password=;"
dim ocn as new system.data.sqlclient.sqlconnection(strcnn)
dim strsql as system.string = "select * from [order details]"
dim ds as system.data.dataset = new system.data.dataset
ocn.open()
dim da as system.data.sqlclient.sqldataadapter = new system.data.sqlclient.sqldataadapter(strsql, ocn)
' 可以直接使用sql语句实现,这个方法可以直接绑定,不需要用模板即可
' cmd.commandtext = "select *,unitprice * quantity *(1- discount) as total from [order details]"
' 我们使用另外一种方法,在datagrid绑定之前为dataset添加一个列
da.fill(ds, "ds")
dim dvwgrid as dataview = ds.tables(0).defaultview
datagrid1.datasource = dvwgrid
datagrid1.databind()
ocn.close()
ocn.dispose()
end sub
private sub datagrid1_itemdatabound(byval sender as object, byval e as datagriditemeventargs) _
handles datagrid1.itemdatabound
dim elemtype as listitemtype = e.item.itemtype
if (elemtype = listitemtype.item or elemtype = listitemtype.alternatingitem) then
'把dataitem转换回datarowview对象
dim mydatarowview as datarowview = ctype(e.item.dataitem, datarowview)
dim mydatarow as datarow = mydatarowview.row
ctype(e.item.cells(0).findcontrol("orderid"), label).text = mydatarow("orderid")
ctype(e.item.cells(1).findcontrol("productid"), label).text = mydatarow("productid")
ctype(e.item.cells(2).findcontrol("unitprice"), label).text = mydatarow("unitprice")
ctype(e.item.cells(3).findcontrol("quantity"), label).text = mydatarow("quantity")
ctype(e.item.cells(4).findcontrol("discount"), label).text = mydatarow("discount")
dim dblprice as double = double.parse(mydatarow("unitprice"))
dim intquantity as integer = int32.parse(mydatarow("quantity"))
dim intdiscount as double = double.parse(mydatarow("discount"))
dim ntotal as string = string.format("{0:c}", dblprice * (1 - intdiscount) * intquantity)
ctype(e.item.cells(5).findcontrol("lbltotal"), label).text = ntotal
end if
end sub
end class
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器