在實際的應用中經常會遇到根據其它列計算某一新列的結果,實現這樣的功能有兩種辦法:一個直接使用sql語句;另外就是在綁定時進行動態添加。第一種方法以前已經介紹過。下面就是第二種方法的具體實現:
adddatasetcolumn.aspx
<%@ page language="vb" autoeventwireup="false" codebehind="adddatasetcolumn.<a target="_blank">asp</a>x.vb"
inherits="<a target="_blank">asp</a>xweb.adddatasetcolumn"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>adddatasetcolumn</title>
<meta name="generator" content="microsoft visual studio .net 7.1">
<meta name="code_language" content="visual basic .net 7.1">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="form1" method="post" runat="server">
<<a target="_blank">asp</a>: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>
<<a target="_blank">asp</a>:templatecolumn headertext="訂單號">
<itemtemplate>
<<a target="_blank">asp</a>:label id="orderid" runat="server"></<a target="_blank">asp</a>:label>
</itemtemplate>
</<a target="_blank">asp</a>:templatecolumn>
<<a target="_blank">asp</a>:templatecolumn headertext="產品號">
<itemtemplate>
<<a target="_blank">asp</a>:label id="productid" runat="server"></<a target="_blank">asp</a>:label>
</itemtemplate>
</<a target="_blank">asp</a>:templatecolumn>
<<a target="_blank">asp</a>:templatecolumn headertext="單價">
<itemtemplate>
<<a target="_blank">asp</a>:label id="unitprice" runat="server"></<a target="_blank">asp</a>:label>
</itemtemplate>
</<a target="_blank">asp</a>:templatecolumn>
<<a target="_blank">asp</a>:templatecolumn headertext="數量">
<itemtemplate>
<<a target="_blank">asp</a>:label id="quantity" runat="server"></<a target="_blank">asp</a>:label>
</itemtemplate>
</<a target="_blank">asp</a>:templatecolumn>
<<a target="_blank">asp</a>:templatecolumn headertext="折扣">
<itemtemplate>
<<a target="_blank">asp</a>:label id="discount" runat="server"></<a target="_blank">asp</a>:label>
</itemtemplate>
</<a target="_blank">asp</a>:templatecolumn>
<<a target="_blank">asp</a>:templatecolumn headertext="總計">
<itemtemplate>
<<a target="_blank">asp</a>:label id="lbltotal" runat="server"></<a target="_blank">asp</a>:label>
</itemtemplate>
</<a target="_blank">asp</a>:templatecolumn>
</columns>
</<a target="_blank">asp</a>: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
新聞熱點
疑難解答