在實際的應用中經常會遇到根據其它列計算某一新列的結果,實現這樣的功能有兩種辦法:一個直接使用SQL語句;另外就是在綁定時進行動態添加。第一種方法以前已經介紹過。下面就是第二種方法的具體實現:
AddDataSetColumn.ASPx
<%@ Page Language="<a href="http://dev.21tx.com/language/vb/" target="_blank">VB</a>" AutoEventWireup="false" Codebehind="AddDataSetColumn.ASPx.vb"
Inherits="ASPx<a href="http://dev.21tx.com/web/" target="_blank">Web</a>.AddDataSetColumn"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN">
<Html>
<HEAD>
<title>AddDataSetColumn</title>
<meta name="GENERATOR" content="Microsoft Visual Studio <a href="http://dev.21tx.com/dotnet/" target="_blank">.Net</a> 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .Net 7.1">
<meta name="vs_defaultClIEntScript" content="<a href="http://dev.21tx.com/web/Javascript/" target="_blank">JavaScript</a>">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/IE5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<ASP: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>
<ASP:TemplateColumn HeaderText="訂單號">
<ItemTemplate>
<asp:Label ID="OrderID" Runat="server"></ASP:Label>
</ItemTemplate>
</ASP:TemplateColumn>
<ASP:TemplateColumn HeaderText="產品號">
<ItemTemplate>
<asp:Label ID="ProductID" Runat="server"></ASP:Label>
</ItemTemplate>
</ASP:TemplateColumn>
<ASP:TemplateColumn HeaderText="單價">
<ItemTemplate>
<asp:Label ID="UnitPrice" Runat="server"></ASP:Label>
</ItemTemplate>
</ASP:TemplateColumn>
<ASP:TemplateColumn HeaderText="數量">
<ItemTemplate>
<asp:Label ID="Quantity" Runat="server"></ASP:Label>
</ItemTemplate>
</ASP:TemplateColumn>
<ASP:TemplateColumn HeaderText="折扣">
<ItemTemplate>
<asp:Label ID="Discount" Runat="server"></ASP:Label>
</ItemTemplate>
</ASP:TemplateColumn>
<ASP:TemplateColumn HeaderText="總計">
<ItemTemplate>
<asp:Label ID="lblTotal" Runat="server"></ASP:Label>
</ItemTemplate>
</ASP:TemplateColumn>
</Columns>
</ASP: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