2

我无法让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

这给了我一条错误消息:

你调用的对象是空的

我遵循以下页面中的建议并更改了代码: 尝试在 asp 中总计 gridview

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

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

知道我怎样才能让它工作吗?

4

1 回答 1

2

您必须使用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
于 2013-09-16T13:00:46.143 回答