[英]WPF Save Changes in DataGrid to DataTable
我的应用程序要求将在 WPF DataGrid
控件中所做的更改保存回DataTable
。 我已经设法将数据从DataGrid
保存到DataTable
,但是,从DataGrid
保存的数据没有显示我所做的更改,它只显示第一次填充DataGrid
时已经存在的数据。
我已经走了这么远:
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();
}
}
这有效,但它不会保存更改,它只是保存DataRow
就像它在DataGrid
中更改之前一样。
我错过了什么吗?
终于找到答案了!。 我必须获取正在更改的DataRow
:
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 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.