
|
Use BindingManagerBase's EndCurrentEdit() before saving data via an Sql/OleDataAdapter
|
|---|
|
Use BindingManagerBase.EndCurrentEdit() before saving data via an Sql/OleDataAdapter
If you are binding a Windows Forms DataGrid to a DataSet and updating it via an Sql/OleDataAdapter, you may want to use the form's BindingManagerBase.EndCurrentEdit() before calling the Adapter's Update() method.
Category: Windows Forms, Visual Studio .NET,
Posted 1/5/2004,
By Mike G.
When a user places a cursor on a DataGrid cell in order to edit its contents, the grid is placed into an Edit mode state.
If the user later clicks on a save button to commit the changes to a database, the changes may not be saved. This
occurs because the DataGrid is still in Edit mode. For this reason, I recommend to all developers that the form, on
which the DataGrid resides, be used to end the Edit mode session of the DataGrid. You can do this by issuing it a
BindingManagerBase.EndCurrentEdit() before saving the data as follows:
private void SaveMethod() {
// This will end any opened edit mode sessions on the DataGrid:
this.BindingContext[this.dgMyDataGrid.DataSource].EndCurrentEdit();
// Now we can save the changes via an Adapter update:
if (dsMyDataSet.GetChanges() != null) {
sqlAdapter.Update(dsMyDataSet.GetChanges());
dsMyDataSet.AcceptChanges();
}
}
|
|