Get Deleted Item in ItemChanging event of BindingList

前端 未结 5 1183
生来不讨喜
生来不讨喜 2020-12-29 22:23

I am using Binding List in my application along with ItemChanged event.

Is there any way I could know the previous values of properties in ItemChanged event. Curren

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 22:42

    Actually, deletion happens before the event fires. So, you cannot get to the item being removed. You definitely need some additional logic for that You can, however, inherit from BindingList, and override RemoveItem:

        public class RemoveAndBind : BindingList
        {
             protected override void RemoveItem(int index)
             {
                if (FireBeforeRemove != null)
                 FireBeforeRemove(this,new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
                base.RemoveItem(index);
             }
    
            public event EventHandler FireBeforeRemove;
        }
    

    Replicate the BindingList constructors. Don't make it cancellable to avoid misconceptions. You may also find some help here: http://connect.microsoft.com/VisualStudio/feedback/details/148506/listchangedtype-itemdeleted-is-useless-because-listchangedeventargs-newindex-is-already-gone

    Hope this helps.

提交回复
热议问题