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?