views:

352

answers:

2

I use data binding to display values in text boxes in WinForms client (C#). When the user clicks Save, I persist my changes to the DB. However, the new value in the active editor is ignored (the previous value is saved). If I tab out of the active editor, then Save, the new value is persisted, as expected.

Is there a way to force the active control to accept its value before persisting?

+1  A: 

This is kind of a hack, but try setting the focus away from the active editor (by setting the focus to something else, like the save button for example) in the button event before you call save.

northpole
+2  A: 

If you can get the Binding instance that corresponds to the input (the TextBox), you can call the WriteValue method to force the value from the control to the object it is bound to.

Also, you can call the EndCurrentEdit method on the BindingManagerBase method on the BindingManagerBase implementation (usually a CurrencyManager) to finish the edit, but that requires implementation of the ICancelAddNew or IEditableObject interface on the object that is bound to (and wouldn't require you to fish for the binding).

casperOne