检测何时在 DataGrid 中编辑行

发布于 2024-12-22 15:51:33 字数 556 浏览 3 评论 0原文

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

我有一个 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...
}

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

这里有没有人知道我该如何做到这一点,或者可以为我指出一个我可能能够找到答案的方向?

I've been trying to google this but have been unable to find a solution that works for me.

I have a DataGrid that is displaying some info from a SQL table that the client dosn't know about.
The client just sends a request to the server and gets a List<SomeClass> as a response that it then displays in a DataGrid.

I need to detect when the user makes change to a row and I need the new values that the user entered.
Currently I'm using RowEditEnding event. And the method that handles this event can then:

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...
}

This gives me the row that was being edited. But it gives me the row before the changes, and I'm unable to figure out how to get the row after the changes happen.

Is there anyone here that knows how I can do this or can point me in a direction where I might be able to find out?

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

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

发布评论

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

评论(2

静若繁花 2024-12-29 15:51:33

请参阅此处的讨论,以避免逐个单元地读出。

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

See the discussion here, to avoid reading out cell-by-cell.

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 });
        }
    }
}
难以启齿的温柔 2024-12-29 15:51:33

就您而言,您正在尝试检测对象的变化。它归结为 SomeClass 的属性,因此您需要关注“Cell”而不是“Row”

假设您的数据网格是 resultGrid,我想出以下代码:

resultGrid.CellEditEnding += resultGrid_CellEditEnding;
void resultGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            var yourClassInstance = e.EditingElement.DataContext;
            var editingTextBox = e.EditingElement as TextBox;
            var newValue = editingTextBox.Text;
        }

“e”还包含有关行和列的信息细胞。因此您将知道单元格正在使用哪个编辑器。在这种情况下,我假设它是一个文本框。
希望有帮助。

In your case, you are trying to detect the change in object. It comes down to the properties of the SomeClass, thus you need to focus on "Cell" instead of "Row"

Assuming your datagrid is resultGrid, i come up with the below code:

resultGrid.CellEditEnding += resultGrid_CellEditEnding;
void resultGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            var yourClassInstance = e.EditingElement.DataContext;
            var editingTextBox = e.EditingElement as TextBox;
            var newValue = editingTextBox.Text;
        }

the "e" also contains information about Row and Column of the Cell. Thus you will know which editor the cell is using. In this case, i assume that it is a textbox.
Hope it help.

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