在vb.net中查找并汇总datagridview列的值

时间:2016-03-28 06:53:08

标签: vb.net

我想使用此代码

在数据网格视图列中查找并汇总搜索到的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

问题是Textbox1只显示一个搜索到的最高行值。例如

id     item name    qty
1      abc           4
2      xyz           10
1      abc           10

Textbox1仅显示结果4。

2 个答案:

答案 0 :(得分:1)

一旦达到第一个值,就退出for语句。因此,你永远不会传递第一个值。擦除退出它应该可以工作。

答案 1 :(得分: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