vb.net – 从绑定列表中获取已删除项目的索引
发布时间:2020-12-17 07:24:31 所属栏目:百科 来源:网络整理
导读:我能够获得添加到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 s
我能够获得添加到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构造函数.此外,不要尝试使其可取消,因为调用者可能会假设调用删除确实删除该项目. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |