【问题标题】:Add total in VB.Net Gridview在 VB.Net Gridview 中添加总计
【发布时间】:2013-09-20 15:14:14
【问题描述】:

我无法让 gridview 在页脚中显示总数

我尝试了以下方法:

<asp:GridView ID="GV" runat="server" 
    DataSourceID="SqlQuery" EmptyDataText="No data">
    <Columns>
        <asp:BoundField DataField="Weekday" FooterText=" " HeaderText="Weekday" />
        <asp:BoundField DataField="Volume" DataFormatString="{0:N0}" 
            FooterText="." HeaderText="Volume" />            
    </Columns>
 </asp:GridView>


Protected Sub GV_rowdatabound(sender As Object, e As GridViewRowEventArgs) Handles GV.RowDataBound
    Dim Volume as integer = 0
    For Each r As GridViewRow In GV.Rows
        If r.RowType = DataControlRowType.DataRow Then
            Volume = Volume + CDec(r.Cells(1).Text)
        End If
    Next
    GV.FooterRow.Cells(1).Text = Math.Round(Volume , 0)
End Sub

这给了我一条错误消息:

对象引用未设置为对象的实例

我按照以下页面中的建议更改了代码: trying to total gridview in asp

Sub GV_WeekSumary_rowcreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    Dim Volume as integer = 0
    For Each r As GridViewRow In GV.Rows
        If r.RowType = DataControlRowType.DataRow Then
            Volume = Volume + CDec(r.Cells(1).Text)
        End If
    Next

    If e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(1).Text = Math.Round(Volume , 0)
    End If
End Sub

这不会出错,但页脚不显示任何值。

我还尝试了以下方法:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim Volume as integer = 0
    For Each r As GridViewRow In GV.Rows
        If r.RowType = DataControlRowType.DataRow Then
            Volume = Volume + CDec(r.Cells(1).Text)
        End If
    Next
    GV.FooterRow.Cells(1).Text = Math.Round(Volume, 0)
    GV.DataBind()
End Sub

页脚中仍然没有值,但是当我调试它时,我可以看到页脚被分配了我需要的值。为什么没有显示在网站上?

知道如何让它工作吗?

【问题讨论】:

    标签: asp.net vb.net gridview footer


    【解决方案1】:

    您必须使用 DataBound 事件。

    试试这个:

    Protected Sub GV_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GV.DataBound
        Dim Volume As Decimal = 0
        For Each r As GridViewRow In GV.Rows
            If r.RowType = DataControlRowType.DataRow Then
                Volume += Convert.ToDecimal(r.Cells(1).Text)
            End If
        Next
        GV.FooterRow.Cells(1).Text = Math.Round(Volume, 0).ToString()
    End Sub
    

    【讨论】:

    • 感谢您的回复。这是一个错字,我更正了。尽管如此,我还是看不到网站上的数据
    • 是的,是的!谢谢你。数据绑定而不是行绑定,然后
    猜你喜欢
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2013-01-23
    • 2015-10-10
    • 2012-07-28
    • 2021-02-05
    相关资源
    最近更新 更多