微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我能够获得添加到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构造函数.此外,不要尝试使其可取消,因为调用者可能会假设调用删除确实删除该项目.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐