Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What are you binding to? If it's a DataSet, DataTable, etc., or better yet, a BindingSource, you should call EndEdit:</p> <pre><code>private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { // Assuming that you are binding to bindingSource1: bindingSource1.EndEdit(); } </code></pre> <p>I implement an <code>ISave</code> interface on my forms to handle dirty state and saving and <strong>whenever</strong> the dirty state is checked (whenever IsDirty is called), I always EndEdit on my binding source:</p> <pre><code>interface ISave { bool IsDirty; bool Save(bool force); } </code></pre> <p>With this interface, when, say, the application 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 <code>ISave</code> and checking the value of <code>IsDirty</code>. Here, I call EndEdit on either the appropriate binding source or, if applicable, the binding control (for example, a grid).</p> <p>And forgive the ramble, but I thought this might be helpful. The rest works like so:</p> <p><code>Save()</code> takes a parameter of "force", so I can have a "Save &amp; 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 <code>Save()</code> 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.</p> <p><code>Save()</code> 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 <kbd>Yes</kbd> or <kbd>No</kbd> and the save itself did not throw an error. Or, <code>Save()</code> returns false in the event the user chose <kbd>Cancel</kbd> or there was an error (in other words, telling the calling code to cancel a form close).</p> <p>How you handle the errors depends on your exception-catching conventions - this could either be caught in the <code>Save()</code> method and displayed to the user or perhaps in an event such as FormClosing where <code>e.Cancel</code> would then be set to true.</p> <p>Used with a form closing event it would look like this:</p> <pre><code>private void form1_FormClosing(object sender, CancelEventArgs e) { if (IsDirty) e.Cancel = !Save(false); } </code></pre> <p>Used with a forced save from a Save &amp; Close button, it would look like this:</p> <pre><code>private void btnSaveAndClose_Click(object sender, EventArgs e) { if (IsDirty) if (Save(true)) Close(); } </code></pre> <p>Anyway, a little more than you asked for, but I hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload