views:

209

answers:

2

When I develop using Delphi I always call DataSet.CheckBrowseMode() to allow bound controls to apply its pending changes to the DataSet. I've been looking for similar way in .NET

I've tried

// When the user press Ctrl-S 
// Apply any pending edit
this.TheDataBindingSource.CurrencyManager.EndCurrentEdit();
// then save the data
this.SaveData();

Unlike Delphi DataSet.CheckBrowseMode(), this code does not has any effect when there are pending changes inside a TextBox. My user has to move the focus away from the textbox via <"TAB"> before pressing Ctrl-S to save the data.

+1  A: 

According to MSDN, if you Validate the Form, it will cause all controls to push their current data back to the data source.

// When the user press Ctrl-S 
// Apply any pending edit
this.Validate();  // assuming this is the Form...
// set the DataSource of the BindingSource to null,
// otherwise, the BindingSource immediately calls
// BeginEdit() automatically
this.TheDataBindingSource.DataSource = null;
this.TheDataBindingSource.EndEdit();
// then save the data
this.SaveData();
Julien Poulin
+1  A: 
this.BindingContext[dataTableVariableHere].EndCurrentEdit();

or if you use BindingSource to bind data to your controls:

foreach(Component c in this.components.Components)
    if(c is BindingSource)
        (c as BindingSource).EndEdit();
Michael Buen
calling EndEdit would make DataRowView (or other IEditableObjecT) push the row to data-store. However, sometime data is pending on the control itself. Calling Form.Validate before BindingSource.EndEdit is the perfect solution.
Sake