wpf - 检测何时在 DataGrid 中编辑行

标签 wpf datagrid wpfdatagrid

我一直在尝试用谷歌搜索这个,但一直无法找到适合我的解决方案。

我有一个 DataGrid,它显示来自 SQL 表的一些客户端不知道的信息。 客户端仅向服务器发送请求并获得 List 作为响应,然后将其显示在 DataGrid 中。

我需要检测用户何时对一行进行了更改,并且我需要用户输入的新值。 目前我正在使用 RowEditEnding 事件。然后处理此事件的方法可以:

private void editRowEventHandler(object sender, DataGridRowEditEndingEventArgs e)
{
    SomeClass sClass = e.Row.DataContext as SomeClass;
    // Send sClass to the server to be saved in the database...
}

这给了我正在编辑的行。但它给了我更改前的行,而我无法弄清楚如何在更改发生后获取该行。

这里有没有人知道我该怎么做,或者可以指出我可以找到的方向?

最佳答案

查看讨论 here , 以避免逐个单元地读取。

private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    if (e.EditAction == DataGridEditAction.Commit) {
        ListCollectionView view = CollectionViewSource.GetDefaultView(dataGrid.ItemsSource) as ListCollectionView;
        if (view.IsAddingNew || view.IsEditingItem) {
            this.Dispatcher.BeginInvoke(new DispatcherOperationCallback(param => 
            { 
                // This callback will be called after the CollectionView
                // has pushed the changes back to the DataGrid.ItemSource.

                // Write code here to save the data to the database.
                return null; 
            }), DispatcherPriority.Background, new object[] { null });
        }
    }
}

关于wpf - 检测何时在 DataGrid 中编辑行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607856/

相关文章:

c# - 根据任务状态(已完成/有故障)链接任务的正确方法

c++ - XPS开源实现?

c# - TextBlock 未显示在 ListView 中

asp.net - 在 asp.net 中按日期过滤 DirectoryInfo 文件

Firebase - 两个离线设备修改相同的数据和更改顺序

c# - 即使集合没有改变,MVVM 从 ViewModel 刷新 Datagrid

c# - 异步,执行简单但耗时的操作

javascript - 以编程方式预先选择 Material-UI 数据网格中的一行(React)

java - EXT GWT 将所选项目绑定(bind)到新的空网格

WPF Datagrid Row Editing "ENDED"事件