3

I've got a form with several textboxes and one datagrid. One business entity can be bound to this form. For example, BO looks like this:

class BO : IEditableObject, INotifyPropertyChanged
{
  public string FirstName {get; set;}
  public string LastName {get; set}
  public BindingList<BO> Relatives {get; set}
  // implementation of the interfaces

}

So on the form, FirstName & LastName are bound to the textboxes and Relatives is bound to the grid. Also on the form I have buttons Save and Cancel. On clicking Save button I call IEditableObject.EndEdit() and on clicking Cancel button I call IEditableObject.CancelEdit(). I want CancelEdit() method to reject all changes made by the user, including changes in Relatives which is bound to the grid. So far so good..

BUT The grid control uses CurrencyManager (the grid is Devexpress control actually but it doesn't matter since I believe WinForms control uses it too). And CurrencyManager calls BeginEdit() & EndEdit() for items in Relatives collection every time the user changes the row. So when button Cancel() is clicked only changes in FirstName & LastName will be cancelled because for the child objects in Relatives collection EndEdit() was already called by the grid's underlying CurrencyManger! So, the question - how to prevent CurrencyManager from calling that methods so that I could reject all changes by one call?

Thanks!

Dmitry
  • 1,220
  • 2
  • 11
  • 18

2 Answers2

2

Your only solution would be to remove the implementation of IEditableObject from your business object. The CurrencyManager (which is used for all bindings in Winforms) does this on its own when the bound object changes.

You'll either have to change your logic to handle higher-level change tracking or remove the interface from your class but leave the methods. Doing this will mean you'll have to call BeginEdit, EndEdit, and CancelEdit explicitly every time.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • Thanks for help. I was thinking about replacing IEditableObject with my own interface too. Will I lose some useful functionality provided by WinForms if I remove IEditableObject? As far as I see it is used mainly in grids. – Dmitry Feb 08 '11 at 16:09
  • @Dmitry: It is used in all binding scenarios. For practical purposes, since grids are usually the only controls to implement cancellation automatically, the only thing you'll lose is the ability to cancel. – Adam Robinson Feb 08 '11 at 16:26
0

If you have a Bindingsource, you can call EndEdit() on the CurrentChanged Event to neutralize the BeginEdit() of the CurrencyManager.

It won't prevent the cost of calling BeginEdit at every change of position.

Marco Guignard
  • 613
  • 3
  • 9