0

我将 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

在此处输入图像描述

问题是页脚没有显示总金额(或任何值)。

我该如何解决?

4

1 回答 1

0

您的行数据绑定不太正确。

每行都会触发 RowDataBound。因此,当您检查 DataRow 时,您从 Total 开始。每次调用都会重置。因此,当您最终到达页脚行时,总计仍为 0。

相反,请执行以下操作:

在页面加载之外声明一个私人小数:

Private myTotal As Decimal = 0

然后在您的数据绑定中添加您的总数并将其存储在total

然后在页脚中使用它。就像是

Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
         Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
         myTotal += (CDec(rowView("item_cost")) * CDec(rowView("item_quantity")))

    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

更新

lblPrice的网格视图中没有。因此,如果您在 myTotal += 价格处设置断点,则您将 0 添加到 0。

请参阅上面的代码以了解获取价格的不同方式。

于 2013-04-16T15:35:08.460 回答