Missing RemovedItem from BindingList and BindingSource
If you work with Windows Forms you cannot live without binding :D, but something is missing from the base structure. I want to create a simple form with a BindingSource, then bind a list of object to a grid and some textboxes to create a master detail form, moreover I set up a simple navigator to make simple for the user navigating the structure, the navigator has the addnew and remove button and the user likes them.
The user can Add and Remove element from the navigator, or directly from a DataGridView, and I simply need to call the appropriate method of Repository to save and delete objects. This structure is really clean, but you encounter a problem: nor the BindingSource nor the BindingList<T> raises a RemovingItem event. I do not know why these classes are designed in this way, you have an AddingNew Event in the Binding Source and BindingList<T> but no corresponding RemovingItem. You have a ListChanged event that tells you when an element is removed, but the element is already gone from the list, so you cannot use to detect object removal.
If you look at the events of BindingList<T> you can notice that there is no way to be informed of the exact element that gets removed. A simple solution is to inherit from the BindingList<T>
|
|
As you can see implementing such a functionality is a breeze, you should simply override the RemoveItem method and raise a custom RemovingItem event. Now I can use this new BindingList in this way.
|
|
I gets all the element from the repository, then I wrap then in a UIProxy (I’ll explain this in a future post), then I intercept the addingNew and RemovingItem events simply calling Save and Delete methods from the repository. Finally I set the BindingList as the source of the BindingSource created by the designer.
I wonder why this event is missing from the basic BindingList<T> implementation.
alk.