简体   繁体   中英

WPF Save Changes in DataGrid to DataTable

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(...);
}

进行更改后,您需要调用AcceptChanges

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM