Обновление элемента в datagridview visualbasic 2010 - PullRequest
0 голосов
/ 07 января 2019

Как я могу обновить элемент в 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

1 Ответ

0 голосов
/ 07 января 2019

Вы должны заменить:

    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

Значение ячейки не только для чтения, но может быть изменено.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...