我将 ASP.NET (VB.NET) 与 SQL-Server-2012 一起使用。
我正在使用 GridView 在名为project_items
.
我添加了一个 FooterTemplate 以便我可以显示一列中所有记录的总量。
这就是我所做的:
<asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="False" CellPadding="4" Font-Names="Tahoma" ForeColor="#333333" GridLines="None" ShowFooter="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="item_name" HeaderText="Item" SortExpression="item_name" />
<asp:BoundField DataField="item_cost" HeaderText="Cost (inc. VAT)" SortExpression="item_cost" />
<asp:BoundField DataField="item_quantity" HeaderText="Quantity" SortExpression="item_quantity" />
<asp:TemplateField HeaderText="Sub-Total (inc. VAT)">
<ItemTemplate>
<asp:Label ID="TextBox3" runat="server"
Text='<%# Convert.ToInt32(Eval("item_quantity")) * Convert.ToDouble(Eval("item_cost"))%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalPrice" runat="server" />
</FooterTemplate>
<FooterTemplate>
<asp:Label ID="lblPrice" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT
items.item_name, items.item_cost, project_items.item_quantity
FROM items
INNER JOIN project_items ON items.item_id = project_items.item_id
WHERE project_items.project_id = @parameter">
<SelectParameters>
<asp:SessionParameter Name="parameter" SessionField="ProjectID" />
</SelectParameters>
</asp:SqlDataSource>
VB.NET 代码:
Imports System.Data.SqlClient
Imports System.Data
Partial Class ProjectReport
Inherits System.Web.UI.Page
Private myTotal As Decimal = 0
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim ProjectID = Session("project_id")
Session("ProjectID") = ProjectID
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim query As New SqlCommand("SELECT Items.item_name, Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = @parameter", conn)
query.Parameters.AddWithValue("@parameter", Convert.ToInt32(Session("ProjectID")))
Dim da As New SqlDataAdapter(query)
da.SelectCommand = query
Dim table As New DataTable()
da.Fill(table)
grdItems.DataSource = table
grdItems.DataBind()
End Sub
Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblPrice As Label = DirectCast(e.Row.FindControl("lblPrice"), Label)
Dim price As Double = CDec(lblPrice.Text)
myTotal += price
End If
If e.Row.RowType = DataControlRowType.Footer Then
Dim lblTotalPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
lblTotalPrice.Text = myTotal.ToString()
End If
End Sub
End Class
问题是页脚没有显示总金额(或任何值)。
我该如何解决?