如何从gridview中的列生成总计并将其显示在VB的标签中? [英] How do I generate the total from a column in gridview and display it in a label for VB?

查看:53
本文介绍了如何从gridview中的列生成总计并将其显示在VB的标签中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB的新手并且卡住!!



我在网上提出了很多建议,但它似乎不符合标签特定的要求产生一个总计,我看到很多gridview页脚示例。



我想要做的是生成一个运行总计,当新条目生成时将更新在gridview中。在每个文本框相关字段中输入数据后,在我的gridview中,只为p1,Textbox2和Textbox6生成了3列。添加新条目后,它将在新行上生成,依此类推。



我要找的是每当新条目生成时,Label7(我正在处理的那个)将显示第3列的运行总计(textbox6.text )来自gridview的值。





最后我想说所有设备的运行总量是...... br />


谢谢!



我的尝试:



Private Sub Label7_TextChanged(发送者为对象,e为EventArgs)处理Label7.TextChanged



Dim s As Double

Dim total As String = 0



For i As Integer = 0 To DataGridView1.RowCount - 1

s = DataGridView1 .Rows(i).Cells(3).Value

total = total + Val(s)

Next

Label7.Text =所有设备的运行总计为:+ total

End Sub

New to VB and stuck!!

I have went over many suggestions on the web, but it doesn't seem to cater to a label specific request for producing a grand total, I see a lot of gridview footer examples.

What I am trying to do is generate a running total that will update when a new entry is made in the gridview. Once I enter the data in each textbox related field, in my gridview only 3 columns are generated for ap1, Textbox2, and Textbox6. Once I add a new entry, it will generate on a new row, so on and so forth.

What I am looking for is whenever a new entry is made, Label7 (the one I am working on) will display the running total for column 3 (textbox6.text) values from the gridview.


At the end I want it to say "Running total for all appliances is..."

Thank you!

What I have tried:

Private Sub Label7_TextChanged(sender As Object, e As EventArgs) Handles Label7.TextChanged

Dim s As Double
Dim total As String = 0

For i As Integer = 0 To DataGridView1.RowCount - 1
s = DataGridView1.Rows(i).Cells(3).Value
total = total + Val(s)
Next
Label7.Text = "Running total for all appliances is: " + total
End Sub

推荐答案

< a href =https://www.codeproject.com/script/Membership/View.aspx?mid=13468713>会员13468713 - 专业档案 [ ^ ]写道:

EDITED ::



我已经完成并重新格式化了我的大部分编码,我现在唯一的问题是



附加信息:索引超出范围。必须是非负数且小于集合的大小。



EDITED::

I have went through and reformatted much of my coding the only problem I have now is

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

Function UpdateRunningTotal()
Dim s As Double
Dim runningTotal As String = 0

For i As Integer = 0 To DataGridView1.RowCount
s = DataGridView1.Rows(i).Cells(2).Value
runningTotal = runningTotal + Val(s)
Next
Return runningTotal
End Function





它不会在我的gridview中生成任何值以及我的标签7 ..



It wont generate any values in my gridview as well in my label7..





如果索引超出范围。必须是非负数且小于集合的大小。错误您必须将代码更改为:



In case of Index was out of range. Must be non-negative and less than the size of the collection. error you have to change your code to:

For i As Integer = 0 To DataGridView1.RowCount -1
...
Next



如果datagridview行的集合从零开始,则必须以RowCount-1结尾。


If collection of datagridview rows is starting from zero, it must ends with RowCount-1.


这篇关于如何从gridview中的列生成总计并将其显示在VB的标签中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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