WPF 将 DataGrid 中的更改保存到 DataTable

发布于 2024-08-14 05:32:07 字数 749 浏览 8 评论 0原文

我的应用程序要求将 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 中更改之前的状态。

我错过了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

池木 2024-08-21 05:32:07

终于找到答案了!我必须获取正在更改的 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(...);
}

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(...);
}
↘紸啶 2024-08-21 05:32:07

您需要在完成后调用 AcceptChanges你的改变。

You need to call AcceptChanges after you make your changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文