title: c# Winforms: DataGridView leave event not getting value of last updated cell
body: <p>I have a WinForms screen with a <code>DataGridView</code> on it and Back/Next buttons. On <code>FormLoad</code>, the grid is populated with values from an XML document. The user can then change the value of any cell in the grid. I've created a <code>SaveGridValuesToXml</code> method which updates the XML file with the updated values from the grid cells. </p>
  
  <p>This mostly works okay, and I have fired the <code>SaveGridValuesToXml</code> method from the Leave event on the grid, so when the user clicks back or next and the grid loses focus, the save method is called. </p>
  
  <p>The problem I have is that all the cell values are updated and reflected correctly in the XML file, EXCEPT for the cell which had focus when the grid loses focus. For example, in a three column grid, if I update the first, second and third cells in the first row, when the grid loses focus only the changes I made in the first and second cells are saved to my XML file. </p>
  
  <p>Code examples are as follows - the<code>LoadXmlDataIntoGridGridView()</code> method is called when the parent form is loaded.</p>
  
  <pre><code>XmlDataDocument xmlData = new XmlDataDocument();
  
  private void LoadXmlDataIntoGridGridView()
  {        
      xmlData.DataSet.ReadXml(@"C:\testFile.xml");
      uxMappingDataGridView.DataSource = xmlData.DataSet;
      uxMappingDataGridView.DataMember = "Item";
  }
  
  private void uxBackButton_Click(object sender, EventArgs e)
  {
      this.Hide();
      uxSettingsUserControl settingsScreen 
         = ParentForm.Controls["uxSettingsUserControl"] as uxSettingsUserControl;
      settingsScreen.Show();
  }
  
  private void uxNextButton_Click(object sender, EventArgs e)
  {
      this.Hide();
      uxLoadScriptUserControl loadScriptScreen 
         = ParentForm.Controls["uxLoadScriptUserControl"] as uxLoadScriptUserControl;
      loadScriptScreen.Show();
  }
  
  private void uxMappingDataGridView_Leave(object sender, EventArgs e)
  {
      SaveGridValuesToXml();
  }
  
  private void SaveGridValuesToXml()
  {   
      xmlData.DataSet.WriteXml(@"C:\testFile.xml");
  }
  </code></pre>
  
  <p>Any suggestions?</p>