vb.net - 更新 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
解决方案
您将不得不更换:
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
单元格的值不是只读的,但可以更改。
推荐阅读
- javascript - 您的会话已过期。在 ajax 表单上发布 laravel
- javascript - ReactJS 为子组件添加回调函数
- c# - 将 SqlParameters 的动态数组传递到 SQL 字符串 IN 子句
- docker - Docker-Copmose.yml 环境变量默认值计算
- groovy - GebSpec 如何隐藏字段 _browser 以及 $spock_sharedField__browser 有什么用?
- dart - 关闭小部件后导航到新页面会引发异常
- laravel - Where Not Group Of Where 语句
- express - 不可订阅的 RxJS 间隔
- java - 在 Eclipse 中的 WindowBuilder 中出现错误,因为数字以不同的语言显示
- database - IMP-00009 和 IMP-00018:导出文件异常结束 前表部分导入已完成:导入 10865925 行