Search code examples
c#xmlwinformsdatagridviewxmldataset

c# Winforms: DataGridView leave event not getting value of last updated cell


I have a WinForms screen with a DataGridView on it and Back/Next buttons. On FormLoad, 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 SaveGridValuesToXml method which updates the XML file with the updated values from the grid cells.

This mostly works okay, and I have fired the SaveGridValuesToXml 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.

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.

Code examples are as follows - theLoadXmlDataIntoGridGridView() method is called when the parent form is loaded.

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");
}

Any suggestions?


Solution

  • This is due to currently editing cell not being committed when you leave the grid by clicking on a button. 1

    To get that last value you need to force the changes to commit by calling EndEdit() on the grid.

    In the code below I call it from SaveGridValuesToXml() - in your example during the leave handler would work also, no significant difference between the two.

    private void uxMappingDataGridView_Leave(object sender, EventArgs e)
    {
        SaveGridValuesToXml();
    }
    
    private void SaveGridValuesToXml()
    {   
        uxMappingDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
        xmlData.DataSet.WriteXml(@"C:\testFile.xml");
    }
    

    1 Not entirely sure why it works this way - personally feels like a bug but maybe someone on the DataGridView product team would say it was a feature.