WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL

In my recent codeproject article on the DataGrid I described a number of techniques for handling the updates to DataTables which are bound to the grid. These examples all worked on the assumption that you want to keep your database synchronised with the DataGrid, with changes being committed on a row-by-row basis, i.e. when the user finishes editing a row the changes are written to the database. The WPF DataGrid operates in a row-oriented manner making this a relatively straightforward scenario to implement.

However, what if you want to commit changes on a cell-by-cell basis? Firstly, lets have a look at the problem in a bit more detail. The following code displays a DataGrid, together with a ‘details‘ view. Note that IsSynchronizedWithCurrentItem is set to true so that the details view and currently selected item within the grid will remain synchronised:

<DockPanel DataContext="{Binding Source={StaticResource PersonData}}">

  <Border DockPanel.Dock="Bottom" Padding="10">
    <Grid x:Name="RootElement">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="1.8*"/>
      </Grid.ColumnDefinitions>

      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>

      <Label Content="Forename:"/>
      <TextBox Grid.Column="1" Text="{Binding Forename}"/>

      <Label Grid.Row="1" Content="Surname:"/>
      <TextBox  Grid.Row="1" Grid.Column="1" Text="{Binding Surname}"/>

      <Label Grid.Row="2"  Content="Age:"/>
      <TextBox Grid.Row="2"   Grid.Column="1" Text="{Binding Age}"/>
    </Grid>
  </Border>

  <dg:DataGrid ItemsSource="{Binding}" Name="dataGrid"
         IsSynchronizedWithCurrentItem="true"/>
</DockPanel>

The ‘PersonData‘ in this case is a DataTable that is constructed in the code-behind.

Now, with this example, if you make changes to a persons surname, then click on the age cell and make changes there, the changes in surname are not reflected in the details view below:

In the above example the user has entered the surname "Blunt" and has moved onto the age cell, however the changes are not reflected in the details view below.

Why is this?

The reason is that when you bind to a DataTable, you are actually binding to your DataTable‘s DefaultView, which is of type DataView. As a result, each row of your table will be bound to a DataRowView. If you look at the documentation for DataRowView you will find that it implements the IEditableObject interface which is the significant factor here. This interface allows you to perform trasnactional changes to your object, i.e. you can change the object‘s properties within a ‘transaction‘, then commit then all in a single atomic action. By default, when you bind to a DataGrid this occurs when the user finishes editing a row, either by moving focus or hitting Enter. In order to allow cell-by-cell changes, we need to commit each time the user moves from one cell to the next in the currently selected row.

The DataGrid exposes a CellEditEnding event which looks like a good candidate for this, from the event arguments we can locate the current EditingElement (i.e. the TextBox which now occupies or cell that is in edit mode), the cell‘s Column and Row, and from here we can locate the Row.Item whcih is our bound DataRowView. You might think that we can just commit the change in this event handler, however, this is an ‘Ending‘ event, not an ‘Ended‘ event. In other words the value has not yet been written to the row. This catches me out far too often - as does its RowEditEnding counterpart!

So, if we cannot commit the edit here, how about the CurrentCellChanged event? however this event does not tell us which cell we just left. Although a simple combination of the two provides the effect we are after:

private DataRowView rowBeingEdited = null;

private void dataGrid_CellEditEnding(object sender,
                                  DataGridCellEditEndingEventArgs e)
{
    DataRowView rowView = e.Row.Item as DataRowView;
    rowBeingEdited = rowView;
}

private void dataGrid_CurrentCellChanged(object sender, EventArgs e)
{
    if (rowBeingEdited != null)
    {
        rowBeingEdited.EndEdit();
    }
}

With this change in place, changes are committed cell-by-cell and the two view remain synchronised:

You can download an example project, wpfdatagridcellbycellcommits, changing the file extension from .doc to .zip.

Regards, Colin E.

时间: 2024-10-02 09:05:21

WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL的相关文章

xceed wpf datagrid

<!--*********************************************************************************** Extended WPF Toolkit Copyright (C) 2007-2013 Xceed Software Inc. This program is provided to you under the terms of the Microsoft Public License (Ms-PL) as publis

WPF DataGrid显格式

Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL    4.83 (13 votes) 1 2 3 4 5 4.83/5 - 13 votes μ 4.83, σa 0.93 [?] Rate: Add a reason or comment to your vote: x Votes of 3 or less require a comment Using Style and Bin

WPF DataGrid 之数据绑定

1. Auto generation of columns 最简单的方法莫过于让DataGrid根据数据源中的字段自动生成列了: 根据实体类的公共属性, 能够自动生成四种类型的数据列,对应关系如下: TextBox columns for string values; CheckBox columns for boolean values; ComboBox columns for enumerable values; Hyperlink columns for Uri values; 拖个Da

编写 WPF DataGrid 列模板,实现更好的用户体验

Julie Lerman 下载代码示例 最近我在为一个客户做一些 Windows Presentation Foundation (WPF) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用这些工具,这样做是为了体验那些坚持使用 Visual Studio 安装附带工具的开发人员会面临什么样的难题. 祝我好运吧!我们来研究一下 WPF DataGrid. 即便有 Web 搜索的帮助和来自在线论坛的建议,仍然有一些用户体验问题花了我几天时间才解决. 将 DataGrid 列分解为成对的互

遍历WPF DataGrid单元格

using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; namespace Splash.WPF { public static class DataGridPlus { /// <summary> /// 获取DataGrid控件单元格 /// </summary> /// <param name="dataGrid&q

WPF DataGrid自定义样式

WPF DataGrid自定义样式 微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. 在DataGrid中的最高水平,你可以改变的外观和感觉,通过设置一些: Property Type Values Default AlternatingRowBackground Brush Any Brush Null Background Brush Any

wpf dataGrid 选中行 失去焦点时 的背景颜色的更改

关于 wpf dataGrid 选中行 失去焦点时 的背景颜色的更改.很简单的方式,在datagrid的resource中更改InactiveSelectionHighlightBrushKey属性的值即可. 关键代码如下: <DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow

[WPF]DataGrid C#添加右键弹出选择菜单

private void dataGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { ContextMenu context = new ContextMenu(); MenuItem item = new MenuItem(); item.Header = "点击删除该行数据"; item.Click += new RoutedEventHandler(item_Click); context.Item

WPF datagrid 初学

<Window x:Class="WpfDemo.WinDataGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:assembly="clr-namespace:System;assembly=mscorlib&quo