从绑定列表中获取已删除项目的索引

发布于 2024-08-01 18:46:07 字数 1083 浏览 6 评论 0原文

我能够获取添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似梦非梦 2024-08-08 18:46:07

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

但是,您可以从 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 确实会删除该项目。

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:

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;
}

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.

深爱成瘾 2024-08-08 18:46:07

我对你问题的措辞有点困惑。 但是,如果某个项目已被删除,则该项目将不再被编入索引。

如果您需要该项目在被删除之前所在的索引,也许存储一个静态变量,例如 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文