在 vb.net 中查找并求和 datagridview 列的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36257445/
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
Find and sum value of a datagridview column in vb.net
提问by Abdul Jabbar
I want to find and sum qty value of searched id in data grid view column am using this code
我想在使用此代码的数据网格视图列中查找和求和搜索 id 的数量值
Dim tqty As Double
For Each row As DataGridViewRow In dgv.Rows
If row.Cells.Item(0).Value = cmbItemCode.Text Then
tqty += row.Cells.Item(4).Value
Textbox1.text=tqty
Exit For
End If
Next
The problem is that Textbox1shows only one top searched row value. For example
问题是Textbox1只显示一个搜索最多的行值。例如
id item name qty
1 abc 4
2 xyz 10
1 abc 10
Textbox1shows the Result only 4.
Textbox1仅显示结果 4。
回答by Haim Katz
As soon as you hit the first value, you exit the for statement. Therefore you never get pass the first value. Erase the Exit For it should work.
一旦您达到第一个值,就退出 for 语句。因此,您永远不会通过第一个值。擦除退出因为它应该可以工作。
回答by Claudio
If DataGridView2.RowCount > 1 Then Dim tqty As Integer = 0
如果 DataGridView2.RowCount > 1 那么 Dim tqty As Integer = 0
'if you have the other column to get the result you could add a new one like these above
For index As Integer = 0 To DataGridView2.RowCount - 1
amount += Convert.ToInt32(DataGridView2.Rows(index).Cells(2).Value)
'if you have the other column to get the result you could add a new one like these above (just change Cells(2) to the one you added)
Next
TextBox1.Text = tqty

