Deleting multiple rows on a databound DynamicRootList not working

Deleting multiple rows on a databound DynamicRootList not working

Old forum URL: forums.lhotka.net/forums/t/7915.aspx


mikemir8 posted on Friday, October 30, 2009

I have a dynamic root list bound to a datagridview in a windows application. My problem is that when deleting multiple rows from the datagrid only one object in the list is actually deleted. No exception is thrown; It's just that the other objects in the list never receive the RemoveItem call.

I did some testing and found that the culprit seems to be in the following sequence:

If I skip the call to OnPropertyChanged(), then I get the following call to RemoveItem (for the following deleted row). But if OnPropertyChanged() is called, then there is no second call to RemoveItem.

My understanding of the plumbing of databinding is not as good as I wish, so any help trying to figure this out is much appreciated. It seems to me that the call to OnPropertyChanged somehow tells the datagrid to stop the subsequent calls to RemoveItem. Is this a bug or is it by design? Any workaround?

By the way, I posted this question a few days ago but apparently the subject I used didn't attract much attention =), that's why I'm reposting it.

Thanks,
- Miguel

RockfordLhotka replied on Friday, October 30, 2009

Interesting.

MarkDirty() calls OnUnknownPropertyChanged() because in any other situation that's a good thing - it allows the UI to know that the object's state has changed.

In this case it is obviously causing you a problem, because that PropertyChanged event is handled by the collection, which raises a ListChanged event as a result, which probably short-circuits the grid control's sequence of remove calls.

I suspect that entirely eliminating the OnUnknownPropertyChanged() call from MarkDirty would result in several other UI scenarios breaking. All that behavior came into being in the early days of .NET and data binding - so 7 or 8 years ago - but I remember putting it there to address something :)

On the other hand, you could, as a test anyway, try overriding OnUnknownPropertyChanged() in your particular class and have it do nothing. That'd prevent the event from being raised and might solve the issue?

Copyright (c) Marimer LLC