欢迎光临
我们一直在努力

如何在DataGrid绑定之前为DataSet添加新列-.NET教程,数据库应用

建站超值云服务器,限时71元/月

在实际的应用中经常会遇到根据其它列计算某一新列的结果,实现这样的功能有两种办法:一个直接使用sql语句;另外就是在绑定时进行动态添加。第一种方法以前已经介绍过。下面就是第二种方法的具体实现:

adddatasetcolumn.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="adddatasetcolumn.<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>x.vb&#8221;<br /> inherits=&#8221;<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>xweb.adddatasetcolumn&#8221;%><br /> <!doctype html public "-//w3c//dtd html 4.0 transitional//en"><br /> <html><br /> <head><br /> <title>adddatasetcolumn</title><br /> <meta name="generator" content="microsoft visual studio .net 7.1"><br /> <meta name="code_language" content="visual basic .net 7.1"><br /> <meta name="vs_defaultclientscript" content="javascript"><br /> <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5"><br /> </head><br /> <body ms_positioning="gridlayout"></p> <form id="form1" method="post" runat="server"> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:datagrid id=&#8221;datagrid1&#8243; runat=&#8221;server&#8221; autogeneratecolumns=&#8221;false&#8221;<br /> showfooter=&#8221;true&#8221; width=&#8221;100%&#8221;><br /> <headerstyle font-names="宋体" font-bold="true" horizontalalign="center" forecolor="navy" backcolor="lightsalmon"></headerstyle> <footerstyle backcolor="#ffccff"></footerstyle> <columns> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn headertext=&#8221;订单号&#8221;><br /> <itemtemplate><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label id=&#8221;orderid&#8221; runat=&#8221;server&#8221;></<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label><br /> </itemtemplate><br /> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn headertext=&#8221;产品号&#8221;><br /> <itemtemplate><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label id=&#8221;productid&#8221; runat=&#8221;server&#8221;></<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label><br /> </itemtemplate><br /> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn headertext=&#8221;单价&#8221;><br /> <itemtemplate><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label id=&#8221;unitprice&#8221; runat=&#8221;server&#8221;></<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label><br /> </itemtemplate><br /> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn headertext=&#8221;数量&#8221;><br /> <itemtemplate><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label id=&#8221;quantity&#8221; runat=&#8221;server&#8221;></<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label><br /> </itemtemplate><br /> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn headertext=&#8221;折扣&#8221;><br /> <itemtemplate><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label id=&#8221;discount&#8221; runat=&#8221;server&#8221;></<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label><br /> </itemtemplate><br /> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn headertext=&#8221;总计&#8221;><br /> <itemtemplate><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label id=&#8221;lbltotal&#8221; runat=&#8221;server&#8221;></<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:label><br /> </itemtemplate><br /> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:templatecolumn><br /> </columns> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:datagrid><br /> </form> <p> </body><br /> </html><br />

adddatasetcolumn.aspx.vb

imports system.web.ui.webcontrols</p> <p>public class adddatasetcolumn<br /> inherits system.web.ui.page</p> <p>#region &#8221; web 窗体设计器生成的代码 &#8221;</p> <p> 该调用是 web 窗体设计器所必需的。<br /> <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()</p> <p> end sub<br /> protected withevents datagrid1 as system.web.ui.webcontrols.datagrid</p> <p> 注意: 以下占位符声明是 web 窗体设计器所必需的。<br /> 不要删除或移动它。<br /> private designerplaceholderdeclaration as system.object</p> <p> private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init<br /> codegen: 此方法调用是 web 窗体设计器所必需的<br /> 不要使用代码编辑器修改它。<br /> initializecomponent()<br /> end sub</p> <p>#end region</p> <p> private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load<br /> dim strcnn as string = &#8220;data source=.\netsdk; initial catalog=northwind;user id=sa;password=;&#8221;<br /> dim ocn as new system.data.sqlclient.sqlconnection(strcnn)<br /> dim strsql as system.string = &#8220;select * from [order details]&#8221;<br /> dim ds as system.data.dataset = new system.data.dataset<br /> ocn.open()<br /> dim da as system.data.sqlclient.sqldataadapter = new system.data.sqlclient.sqldataadapter(strsql, ocn)<br /> 可以直接使用sql语句实现,这个方法可以直接绑定,不需要用模板即可<br /> cmd.commandtext = &#8220;select *,unitprice * quantity *(1- discount) as total from [order details]&#8221;<br /> 我们使用另外一种方法,在datagrid绑定之前为dataset添加一个列<br /> da.fill(ds, &#8220;ds&#8221;)<br /> dim dvwgrid as dataview = ds.tables(0).defaultview<br /> datagrid1.datasource = dvwgrid<br /> datagrid1.databind()<br /> ocn.close()<br /> ocn.dispose()<br /> end sub</p> <p> private sub datagrid1_itemdatabound(byval sender as object, byval e as datagriditemeventargs) _<br /> handles datagrid1.itemdatabound<br /> dim elemtype as listitemtype = e.item.itemtype<br /> if (elemtype = listitemtype.item or elemtype = listitemtype.alternatingitem) then<br /> 把dataitem转换回datarowview对象<br /> dim mydatarowview as datarowview = ctype(e.item.dataitem, datarowview)<br /> dim mydatarow as datarow = mydatarowview.row<br /> ctype(e.item.cells(0).findcontrol(&#8220;orderid&#8221;), label).text = mydatarow(&#8220;orderid&#8221;)<br /> ctype(e.item.cells(1).findcontrol(&#8220;productid&#8221;), label).text = mydatarow(&#8220;productid&#8221;)<br /> ctype(e.item.cells(2).findcontrol(&#8220;unitprice&#8221;), label).text = mydatarow(&#8220;unitprice&#8221;)<br /> ctype(e.item.cells(3).findcontrol(&#8220;quantity&#8221;), label).text = mydatarow(&#8220;quantity&#8221;)<br /> ctype(e.item.cells(4).findcontrol(&#8220;discount&#8221;), label).text = mydatarow(&#8220;discount&#8221;)</p> <p> dim dblprice as double = double.parse(mydatarow(&#8220;unitprice&#8221;))<br /> dim intquantity as integer = int32.parse(mydatarow(&#8220;quantity&#8221;))<br /> dim intdiscount as double = double.parse(mydatarow(&#8220;discount&#8221;))</p> <p> dim ntotal as string = string.format(&#8220;{0:c}&#8221;, dblprice * (1 &#8211; intdiscount) * intquantity)<br /> ctype(e.item.cells(5).findcontrol(&#8220;lbltotal&#8221;), label).text = ntotal<br /> end if<br /> end sub<br /> end class<br />

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何在DataGrid绑定之前为DataSet添加新列-.NET教程,数据库应用
分享到: 更多 (0)