GridView页脚不显示数据

时间:2013-04-16 15:25:10

标签: asp.net vb.net gridview sql-server-2012

我正在使用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

enter image description here

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你的行数据绑定不是很正确。

每行都会触发RowDataBound。因此,当您检查DataRow时,您将从总计0开始。这将在每次通话时重置。因此,当您最终到达页脚行时,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

<强>更新

您的gridview中没有lblPrice。因此,如果您在myTotal + =价格处设置断点,则需要将0添加为0。

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