BindingList<> notification before delete?

M

Michel Walsh

Hi,

A datagrid has its DataSource set to a BindingList<CustomClass>. While the
ListChanged event fires AFTER a deletion, I am looking for a simple way to
be notified BEFORE the item would be effectively deleted (for performing
extra check and cleanup). Is there a simple trick I miss, because I don't
know how to do that, except to maintain TWO lists, and compare them in the
ListChanged event (to detect which item is now missing).

Simplified examples of what I try to get :
- check before deleting if there will be at least one 'admin' left
- free C# ressources of the CustomClass item just to be deleted (as a
cascade delete would have done for database records)


Thanks,

Vanderghast, Access MVP
 
M

Marc Gravell

Well, you could always write a generic class that inherits from
BindingList<T>, and override the RemoveItem(int) method to fire a new
event before calling base.RemoveItem() [perhaps with a CancelEventArgs
implementation]. This would allow the subscriber to obtain the item (by
index) prior to the delete.

Marc
 
M

Michel Walsh

Hi,


Hoping the datagridview internally calls that method... I'll try it
tomorrow, anyhow, and see what happens.

Vanderghast, Access MVP
 
M

Marc Gravell

Actually, I believe it will call the IList.RemoveAt(int) method, which
is in this case gets forwarded to the Collection<T> base-class of
BindingList<T>, so yes: overriding this method should do what you need.

Only one way to find out ;-p

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top