如何更新 ListView 中现有项目的值?
How to update values of an existing Item in a ListView?
我正在做一个小项目,我必须将具有一些值的项目添加到 ListView 中。
我希望能够在再次添加 Item 时更新它的值,而不是再次读取具有不同详细信息的同一个 Item。
下面是我到目前为止设法想出的代码:
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
我正在做一个小项目,我必须将具有一些值的项目添加到 ListView 中。
我希望能够在再次添加 Item 时更新它的值,而不是再次读取具有不同详细信息的同一个 Item。
下面是我到目前为止设法想出的代码:
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