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

80酷酷网    80kuku.com

  datagrid

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

AddDataSetColumn.aspx

<% Page Language="vb" AutoEventWireup="false" Codebehind="AddDataSetColumn.<a 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

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: