我正在开发一个小项目,我必须在ListView中添加一些带有一些值的项目。 我希望能够更新项目的值,如果它再次添加,而不是再次读取具有不同细节的相同项目。
下面是我迄今为止设法提出的代码:
For Each item as ListViewItem in MainListView.Items
If item.SubItems(0).Text = ItemCode Then
item.SubItems(3).Text += ItemQty
item.SubItems(5).Text += ItemPrice
Else
' normal listview insert codes run here
End If
Next
现在看来,如果Item首先在列表中,但是只有向下移动一步,值才能更新,类似的Item也会使用它自己的记录插入ListView,而不是查找和更新现有的记录。
任何帮助纠正它将不胜感激。谢谢。
循环通过你的ListView
检查ItemCode
是否存在。如果找到,则更新项目并使用Return语句退出sub。可以使用For Each
,因为您不会更改集合,只会更改集合中某个项目的值。如果您在列表视图中添加或删除项目,则无法使用For Each
,因为集合本身正在被更改。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item As ListViewItem In MainListView.Items
If item.SubItems(0).Text = ItemCode Then
item.SubItems(3).Text += ItemQty
item.SubItems(5).Text += ItemPrice
Return
End If
Next
'Now if the For each fails doesn't find the record (fails to run the Return)
' normal listview insert codes run here
End Sub