簡體   English   中英

在vb.net中查找並匯總datagridview列的值

[英]Find and sum value of a datagridview column in 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。

達到第一個值后,立即退出for語句。 因此,您永遠不會超過第一個值。 擦除出口,因為它應該可以工作。

如果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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM