【问题标题】:GridView footer not displaying dataGridView 页脚不显示数据
【发布时间】:2013-04-16 15:25:09
【问题描述】:

我正在使用带有 SQL-Server-2012 的 ASP.NET (VB.NET)。

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

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

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

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

    每行都会触发 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。

    查看上面的代码以了解获取价格的不同方式。

    【讨论】:

    • 我按你说的试过了,页脚还是空的。我错过了什么? (有问题的更新代码)
    • 您好,打扰您了。同样,页脚未显示。我的 ASP 代码有问题吗?
    • 页脚正在显示;你可以在你的图像中看到这一点。问题是您试图将一个不存在的数字相加。您的网格视图中没有lblPrice,因此它会将零添加到myTotal
    • 尝试在你的代码中添加一些断点,这样你就可以准确地看到被调用的内容和设置的总数。另请参阅我的答案中的更新代码,它向您展示了如何从您的 dataitem 获取 item_cost
    • 对不起,我不明白。我添加了一个 lblPrice 但仍然没有显示任何内容(请参阅更新的代码)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 2016-10-16
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    相关资源
    最近更新 更多