My application requires that changes made in the WPF DataGrid
control are saved back to the DataTable
. I have managed to save data from the DataGrid
to DataTable
, however, the data saved from the DataGrid
does not show the changes I have made, it just shows the data that was already there when the DataGrid
was first populated.
I have got this far:
public void UpdateQueueData(object sender, DataGridRowEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
DataGridRow dgRow = e.Row;
DataRowView rowView = dgRow.Item as DataRowView;
DataRow drItem = rowView.Row;
Queue.Rows.RemoveAt(e.Row.GetIndex());
Queue.ImportRow(drItem);
WriteXML();
}
}
This works but it does not save the changes, it just saves the DataRow
as it was before it was changed in the DataGrid
.
Am I missing something?
Finally found the answer!. I have to Get the DataRow
that was being changed:
private void dataGrid_CurrentCellChanged(object sender, EventArgs e)
{
DataTable dt = ((DataView)dataGridQueue.ItemsSource).ToTable();
// Set the value of my datatable to the the changed version before
// writing it so a file.
dt.WriteXMLScema(...);
}