GridView的脚注不显示数据 [英] GridView footer not displaying data

查看:142
本文介绍了GridView的脚注不显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET(VB.NET)与SQL服务器2012年。

I'm using ASP.NET (VB.NET) with SQL-Server-2012.

我使用一个GridView来显示名为 project_items

I'm using a GridView to display data in table called project_items.

我添加了一个FooterTemplate,这样我可以在一列中显示的所有记录的总金额。

I added a FooterTemplate so that I could display the total amount of all the records in a column.

这是我做过什么:

<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 code:

VB.NET Code:

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

的问题是,在页脚不显示的总量(或任何值)。

The problem is that the footer is not displaying the total amount (or any values).

我该如何解决这个问题?

How do I fix this?

推荐答案

您行数据绑定是不完全正确。

Your row data bound isn't quite right.

本的RowDataBound被解雇的每一行。因此,当你检查一个DataRow你出发为0的总这让在每次调用复位。所以,当你终于绕到尾行总还是为0。

The RowDataBound gets fired for every row. So when you're checking a DataRow you're starting off with a Total of 0. This is getting reset on every call. So when you're finally around to the Footer row Total is still 0.

相反,做到以下几点:

声明一个私人十进制:

Private myTotal As Decimal = 0

然后在你的绑定加起来到总额并将其存储在

然后用在你的页脚。类似

Then use that in your footer. Something like

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。

You don't have a lblPrice in your gridview. So if you put a breakpoint at your myTotal += price you're adding 0 to 0.

请参阅code以上不同的方式来让你的代价。

See code above for different way to get your price.

这篇关于GridView的脚注不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆