What are you binding to? If it's a DataSet, DataTable, etc, or better yet, a BindingSource, you should call EndEdit:
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
//assuming that you are binding to bindingSource1:
bindingSource1.EndEdit();
}
I implement an ISave
interface on my forms to handle dirty state and saving and whenever the dirty state is checked (whenever IsDirty is called), I always EndEdit on my binding source:
interface ISave
{
bool IsDirty;
bool Save(bool force);
}
With this inteface, when, say, the app is shutting down, I can easily iterate through my open MdiChild windows checking to see if any information has been saved by casting the child form to ISave
and checking the value of IsDirty
. Here, I call EndEdit on either the appropriate binding source or, if applicable, the binding control (ex: a grid).
And forgive the ramble, but thought this might be helpful. The rest works like so:
Save()
takes a parameter of "force" so I can have a "Save & Close" button a form (saves the user an extra click or confirmation asking if they want to save their changes). If force is false, the Save()
method is responsible for asking the user if they want to save. If it's true, it's assumed the user has already decided they definitely want to save their information and this confirmation is skipped.
Save()
returns bool- true if it's safe to continue executing the calling code (presumably a Form_Closing event). In this case, (if force was false), given a YesNoCancel MessageBox, the user either selected Yes or No and the save itself did not throw an error. Or, Save()
returns false in the event the user chose Cancel or there was an error (in other words, telling the calling code to cancel a form close). How you handle the errors depends on your exception catching conventions- this could either be caught in the Save()
method and displayed to the user or perhaps in an event such as FormClosing where e.Cancel
would then be set to true.
Used with a form closing event it would look like this:
private void form1_FormClosing(object sender, CancelEventArgs e)
{
if(IsDirty)
e.Cancel = !Save(false);
}
Used with a forced save from a Save & Close button, it would look like this:
private void btnSaveAndClose_Click(object sender, EventArgs e)
{
if(IsDirty)
If(Save(true))
Close();
}
Anyway, a little more than you asked for, but I hope this helps!