Forcing Data to be written to DataSet

G

Guest

The issue: I have a grid which has a dataset as the datasource.

If a user enters data in a textbox on the grid and then clicks on the close
button of the form (the red X in the upper right corner), no events fire for
the grid or the control that the user was typing in.

The closing event of the form does fire, and I check there to see if any
changes need to be saved. If the user changes rows in the form, the typed
data is transmitted to the dataset. However, if the user goes directly for
the close button, the data is not transmitted to the dataset, and the dataset
does not know there are changes to be saved.

So the question is, how to force the events to fire? In another language,
we would use the SetFocus() method to set the focus to a control on the grid
which would fire the necessary events. But how do I do this in C#?

Greg
 
D

Dave

I'd like to add to Cor's explaination:

The closing event is cancellable, so it may be more appropriate to prompt the user to save changes instead of assuming they want the
changes saved.

protected override void OnClosing(CancelEventArgs e)
{
// Implement a method to check if the form has changes, named "IsDirty"
if (IsDirty() &&
MessageBox.Show(this, "Save Changes",
"You have unsaved changes.\r\nAre you sure you want to close the application?", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning, MessageBoxDefaultButton.2) == DialogResult.No))
{
e.Cancel = true;
}

base.OnClosing(e);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top