首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> 数据库教程
如何在DataGrid绑定之前为DataSet添加新列-.NET教程,数据库应用
作者:网友供稿 点击:19
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

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

adddatasetcolumn.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="adddatasetcolumn.<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>x.vb" inherits="<a href="http://www.chinaitpower.com/dev/web/asp/index.html" 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 href="http://www.chinaitpower.com/dev/web/asp/index.html" 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 href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn headertext="订单号"> <itemtemplate> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label id="orderid" runat="server"></<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label> </itemtemplate> </<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn headertext="产品号"> <itemtemplate> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label id="productid" runat="server"></<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label> </itemtemplate> </<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn headertext="单价"> <itemtemplate> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label id="unitprice" runat="server"></<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label> </itemtemplate> </<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn headertext="数量"> <itemtemplate> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label id="quantity" runat="server"></<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label> </itemtemplate> </<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn headertext="折扣"> <itemtemplate> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label id="discount" runat="server"></<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label> </itemtemplate> </<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn headertext="总计"> <itemtemplate> <<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label id="lbltotal" runat="server"></<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:label> </itemtemplate> </<a href="http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank">asp</a>:templatecolumn> </columns> </<a href="http://www.chinaitpower.com/dev/web/asp/index.html" 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
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·jsp留言板源代码4-JSP教程,数据库相关
·“脱衣秀”泛滥 四川打击淫秽色情视频聊天室
·c#中使用 crystal reports (水晶报表)的打包和部署问题-.NET教程,报表/图形/Office
·MDAC2.8 下载!-ASP教程,数据库相关
·数据库安装包的制作(参考MSDN)-.NET教程,安装和部署
·asp讲座之七:asp与数据库(二)
·深度和广度优先分油问题(C#实现)-.NET教程,C#语言
·C#在状态栏中,自绘进度条,-.NET教程,C#语言
·怎么清除sql server日志
·C#中的接口-.NET教程,C#语言
最新文章
·sql server 2005 ce基础概要_数据库教程
·用flash as代码制作按钮弹出窗口_flash教程
·alexa:戏曲性地调整_alexa排名
·google也推出域名注册_google推广
·技巧总结:div中class与id的区别及应用_css教程
·windows vista命令runas.exe全解析_windows vista
·photoshop将美女照片处理成仿古斑驳油画_photoshop教程
·百度主题推广代码的完全解析-知己知彼_网赚技巧
·你的google adsense帐号是否被人盗用?_网赚技巧
·如何挑出google adsense中单价极低的广告商_网赚技巧
相关主题
  • 如何在DataGrid里面产生滚动条-.NET教程,数据库应用
  • 如何在DataGrid里面使用动态图形表示数字-.NET教程,数据库应用
  • 如何在DataGrid里面产生滚动条而不滚动题头-.NET教程,Asp.Net开发
  • 如何在DataGrid控件中隐藏列-.NET教程,Asp.Net开发
  • 如何在DataGrid控件中实现编辑、删除、分类以及分页操作-.NET教程,Asp.Net开发
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭