首页 > 开发 > 综合 > 正文

如何在DataGrid绑定之前为DataSet添加新列

2024-07-21 02:22:46
字体:
来源:转载
供稿:网友

在实际的应用中经常会遇到根据其它列计算某一新列的结果,实现这样的功能有两种办法:一个直接使用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

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表