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 Relative开发者_StackOverflows 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!
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.
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.
精彩评论