首页 > 解决方案 > 更新 datagridview visualbasic 2010 中的项目

问题描述

如何更新 DataGridView 中的项目?如果项目已经存在,我想更新数量。如果项目不存在,我想添加新行。

这是我的代码:

    Dim price As Integer = 80
    Dim qty As Integer = 1
    Dim total As Integer
    total = qty * price
    Dim food As String = "SISIG"
    Dim index As Integer = 0
    Dim a As Integer
    If DataGridView1.Rows.Count > 0 Then
        For a = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(a).Cells(0).Value = food Then
                qty = CInt(DataGridView1.Rows(a).Cells("QUANTITY").Value) + 1
                index = a

            End If
        Next
    Else
        DataGridView1.Rows.Add(food, qty, price, total)
    End If
    total = qty * price
    DataGridView1.Rows(index).Cells(0).Value = food
    DataGridView1.Rows(index).Cells(1).Value = qty
    DataGridView1.Rows(index).Cells(2).Value = price
    DataGridView1.Rows(index).Cells(3).Value = total

标签: vb.netdatagridview

解决方案


您将不得不更换:

    For a = 0 To DataGridView1.Rows.Count - 1
        If DataGridView1.Rows(a).Cells(0).Value = food Then
            qty = CInt(DataGridView1.Rows(a).Cells("QUANTITY").Value) + 1
            index = a
        End If
    Next

和:

    For a = 0 To DataGridView1.Rows.Count - 1
        If DataGridView1.Rows(a).Cells(0).Value = food Then
            DataGridView1.Rows(a).Cells("QUANTITY").Value += 1
        End If
    Next

单元格的值不是只读的,但可以更改。


推荐阅读