【发布时间】: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