vb.net中datagridview列的查找和求和值
我想使用此代码在数据网格视图列am中查找和求和搜索id的数量值vb.net中datagridview列的查找和求和值,vb.net,Vb.net,我想使用此代码在数据网格视图列am中查找和求和搜索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
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
问题是Textbox1
只显示一个顶部搜索的行值。比如说
id item name qty
1 abc 4
2 xyz 10
1 abc 10
Textbox1
仅显示结果4。只要您点击第一个值,就退出for语句。因此,您永远无法通过第一个值。清除出口,因为它应该可以工作。如果DataGridView2.RowCount>1,则
Dim tqty作为整数=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
他还应该等待设置
TextBox1.Text
,直到循环结束。