Automatic Save() or commit to database when editing BusinessBase in WinForms

Automatic Save() or commit to database when editing BusinessBase in WinForms

Old forum URL:

cwinkelmann posted on Tuesday, December 27, 2011

I have a BusinessBase object in a BusinessBindingListBase. When bound to a DataGridView in WinForms, I would like the BusinessBase to automatically save it's values to the database when the object IsDirty property is set.

What I'm trying to avoid is forcing the end users from having to press a "Save" button every time they alter an object. I am completely aware of the database overhead of committing changes on a property level, but that is the responsiveness they require.

If I tie this to the ListChanged event, then I have to check IsValid before calling Save() or I get an error

      if (!IsValid)
        throw new Rules.ValidationException(Resources.NoSaveInvalidException);

If I check for IsValid and prevent the Save() method from being called then the user will be allowed to create new BusinessBase and delete other BusinessBase objects and modify even others and none of the changes will be saved because nothing can be saved while one child object is not valid... 

I'm presently having terrible difficulties understanding how to integrate a method for saving each BusinessBase in either the BindingListBase or in the BusinessBase itself.

I've also investigated keying off the BusinessBase PropertyChanged event but that has similar problems in that if a row is deleted, it does not report to the parent BindingListBase to remove it from the DeletedList so the call to delete may occur twice on the same object.

Does anyone have advice or ideas where this functionality may be supported in the current CSLA framework?


Thanks greatly,




cwinkelmann replied on Tuesday, December 27, 2011

Is DynamicBindingListBase the class I need to inherit from for this? Curious when this was added to the CSLA framework :-)

We'll see.

RockfordLhotka replied on Tuesday, December 27, 2011

Yes, that is the class you are looking for. It has been around (though with a slightly different name) since around 2005.

ajj3085 replied on Wednesday, December 28, 2011

Yes, DynamicBindingListBase is the class you want to use.

Note though that it doesn't save as each property is changed, it saves when the user is done editing the row.  Typically when a user started editing a row, a little pencil icon appears on the left row indicator.  The user can cancel all changes to the row by presses ESC, or may commit all changes by leaving the row (the row loses focus), or maybe there's a keyboard shortcut. 

I would recommend just using this approach as its pretty standard Windows behavior, and most grid controls emulate this so you'd be fighting the normal databinding to get saves on every property change.

cwinkelmann replied on Wednesday, March 14, 2012

Is there any way for a BusinessBase<T> object to auto-save itself? Where this is not part of a list and not in a DataGridView? Simply bound to a form or UserControl...

JonnyBee replied on Thursday, March 15, 2012

And what should trigger the Save method?

No - there is no builtin support for an auto-save of a Root Object.

Csla.Windows.CslaActionExtender may help but has several known bugs.

Copyright (c) Marimer LLC