在這篇範例裡面,資料庫使用北風資料庫,主要講兩個東西,其實都還蠻基本的
DropDownList動態結合GridView:
透過 GridView的RowDataBound事件計算合計:
在這篇範例裡面,資料庫使用北風資料庫,主要講兩個東西,其實都還蠻基本的
- DropDownList動態結合GridView:
- 使用DropDownList列出訂單編號(Orders.OrderID),當挑選了DropDownList之後,GridView依照DropDownList的訂單編號,顯示該訂單的細部資料(Order Details)
- 透過 GridView的RowDataBound事件,來進行計算AMT合計的工作
接著來看運作的畫面會是如何
一開始出現DropDownList,預設項目【請選擇】,如下圖
選擇其中之一訂單編號,如下圖
出現該訂單的內容,並在最後顯是合計
相關程式內容如下
畫面部分
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="OrderID" DataValueField="OrderID">
</asp:DropDownList>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [OrderID] FROM [Orders] ORDER BY [OrderID]">
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource2"
ShowFooter="True">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"
SortExpression="OrderID" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"
SortExpression="ProductID" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="Discount" HeaderText="Discount"
SortExpression="Discount">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="AMT" HeaderText="AMT" ReadOnly="True"
SortExpression="AMT">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT OrderID, ProductID, UnitPrice, Quantity, Discount, UnitPrice * Quantity - Discount AS AMT FROM [Order Details] WHERE (OrderID = @OrderID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="OrderID"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
CodeFile內容
Partial Class _Default
Inherits System.Web.UI.Page
Dim SumAMT As Integer = 0 '設定全域變數記錄合計
Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.DataBound
If Not IsPostBack Then
Dim tLItm As New ListItem("請選擇", "")
Me.DropDownList1.Items.Insert(0, tLItm)
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(5).Text = FormatNumber(e.Row.Cells(5).Text, 2)
SumAMT += CInt(e.Row.Cells(5).Text.ToString)
End If
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(5).Text = FormatNumber(SumAMT, 2)
End If
End Sub
End Class
原始碼下載:tGVS.zip(0.6MB) (版本:VS2008)
操作過程錄影下載: tGVSum.zip(9.5MB)
以下是簽名:
- 歡迎轉貼本站的文章,不過請在貼文主旨上加上【轉貼】,並在文章中附上本篇的超連結與站名【topcat姍舞之間的極度凝聚】,感恩大家的配合。
- 小喵大部分的文章會以小喵熟悉的語言VB.NET撰寫,如果您需要C#的Code,也許您可以試著用線上的工具進行轉換,這裡提供幾個參考
Microsoft MVP Visual Studio and Development Technologies (2005~2019/6) | topcat Blog:http://www.dotblogs.com.tw/topcat |