gpt4 book ai didi

vb.net - 从绑定(bind)列表中获取已删除项目的索引

转载 作者:行者123 更新时间:2023-12-02 06:48:11 28 4
gpt4 key购买 nike

我能够获取添加到 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# 提供答案......有人吗?

最佳答案

该项目在事件触发之前被删除。这意味着(无需额外代码)您无法找到正在删除的项目。

但是,您可以从 BindingList 继承,并重写RemoveItem:

public class BindingListWithRemoving<T> : BindingList<T>
{
protected override void RemoveItem(int index)
{
if (BeforeRemove != null)
BeforeRemove(this,
new ListChangedEventArgs(ListChangedType.ItemDeleted, index));

base.RemoveItem(index);
}

public event EventHandler<ListChangedEventArgs> BeforeRemove;
}

您还应该复制 BindingList 构造函数。另外,不要尝试使其可取消,因为调用者可能会认为调用 Remove 确实会删除该项目。

关于vb.net - 从绑定(bind)列表中获取已删除项目的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1242901/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com