在 VB.Net Gridview 中添加总计
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18828558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add total in VB.Net Gridview
提问by Selrac
I can not manage to get the gridview display the total in a footer
我无法让 gridview 在页脚中显示总数
I've tried the following:
我尝试了以下方法:
<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
This gives me an error messages:
这给了我一条错误消息:
Object reference not set to an instance of an object
你调用的对象是空的
I followed advice in the following page and I changed the code: trying to total gridview in asp
我遵循了以下页面中的建议,并更改了代码: 尝试在 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
This does not give an error, but the footer does not show any value.
这不会出错,但页脚不显示任何值。
I've tried also the following:
我也试过以下:
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
Still no value in the footer, but when I debbug it I can see that footer is assisgned the value I need. Why it is not displayed in the website?
页脚中仍然没有价值,但是当我调试它时,我可以看到页脚被分配了我需要的值。为什么在网站上不显示?
Any idea how I can get this to work?
知道如何让这个工作吗?
采纳答案by Samiey Mehdi
You must use DataBoundevent.
您必须使用DataBound事件。
Try this:
尝试这个:
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

