从绑定列表中获取已删除项目的索引
我能够获取添加到 BindingList 的项目的索引。 当我尝试获取已删除项目的索引时,我收到错误
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
这是我的代码
Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
For i As Integer = 0 To _assignedSelection.SelectedCount - 1
Dim item As Jurisdiction = CType(_assignedSelection.GetSelectedRow(i), Jurisdiction)
_list.Remove(item)
Next
End Sub
Private Sub list_Change(ByVal sender As Object, ByVal e As ListChangedEventArgs) Handles _list.ListChanged
If (_list.Count > 0) Then
Select Case e.ListChangedType
Case ListChangedType.ItemAdded
_dal.InsertJurisdiction(_list.Item(e.NewIndex))
Case ListChangedType.ItemDeleted
'MsgBox(e.NewIndex.ToString)
_dal.DeleteJurisdiction(_list.Item(e.NewIndex)) <--------HERE
End Select
End If
End Sub
编辑:也欢迎 C# 中的答案....有人吗?
I am able to get the index of items added to the BindingList. When I try to get the index if the deleted item I get the error
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Here is my code
Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
For i As Integer = 0 To _assignedSelection.SelectedCount - 1
Dim item As Jurisdiction = CType(_assignedSelection.GetSelectedRow(i), Jurisdiction)
_list.Remove(item)
Next
End Sub
Private Sub list_Change(ByVal sender As Object, ByVal e As ListChangedEventArgs) Handles _list.ListChanged
If (_list.Count > 0) Then
Select Case e.ListChangedType
Case ListChangedType.ItemAdded
_dal.InsertJurisdiction(_list.Item(e.NewIndex))
Case ListChangedType.ItemDeleted
'MsgBox(e.NewIndex.ToString)
_dal.DeleteJurisdiction(_list.Item(e.NewIndex)) <--------HERE
End Select
End If
End Sub
EDIT: Answers in C# are also welcome....anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该项目在事件触发之前被删除。 这意味着(无需额外代码)您无法找到正在删除的项目。
但是,您可以从 BindingList 继承,并重写 RemoveItem:
您还应该复制 BindingList 构造函数。 另外,不要尝试使其可取消,因为调用者可能会认为调用
Remove
确实会删除该项目。The item is removed before the event fires. This means (without additional code) you cannot get to the item being removed.
You can, however, inherit from BindingList, and override RemoveItem:
You should also replicate the BindingList constructors. Also, don't try to make it cancellable, as callers may assume calling
Remove
does indeed remove the item.我对你问题的措辞有点困惑。 但是,如果某个项目已被删除,则该项目将不再被编入索引。
如果您需要该项目在被删除之前所在的索引,也许存储一个静态变量,例如
Private Shared returnedIndex As Integer
并在删除该项目之前进行设置,将为您提供您想要的?I am a little confused with the wording of your question. However, an item is no longer indexed if it has been removed.
If you need the index that the item was at before it was removed, perhaps storing a static variable, such as
Private Shared removedIndex As Integer
and setting that before you remove the item, will give you what you want?