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.

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

  1. xceed wpf datagrid

    <!--*********************************************************************************** Extended ...

  2. WPF DataGrid显格式

    Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL    4.83 (13 votes) ...

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

    Julie Lerman 下载代码示例 最近我在为一个客户做一些 Windows Presentation Foundation (WPF) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用 ...

  4. WPF DataGrid 控件的运用

    WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...

  5. WPF DataGrid 获取选中 一行 或者 多行

    WPF中DataGrid使用时,需要将其SelectedItem转换成DataRowView进行操作 然而SelectedItem 与SelectedItems DataGrid的SelectionU ...

  6. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

  7. WPF DataGrid常用属性记录

    WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...

  8. cell与cell之间的间距问题,以及section跟随屏幕滑动而滑动问题

    苹果在cell与cell之间默认没有间距,这样有时候不能满足我们界面要求,所以我们就需要将cell设置为分组模式(也就是每组一行或者多行,分为n组),然后我们就可以在代理中根据自己的需求设计cell之 ...

  9. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

随机推荐

  1. hdu2108(判断凸多边形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2108 题意: 给出一个多边形的所有顶点,判断是不是凸多边形: 思路: 判断凸多边形的方法比较多,如:若 ...

  2. eclipse查看hadoop中文件出现乱码

    出现这个问题, 我首先去找了一下几个问题: 1.文件是否是utf-8 2.上传到Linux中的hadoop, 在Linux下去查看是否乱码 3.上面都没有问题, 就去检查eclipse,将项目工程改成 ...

  3. Gradle使用指南

    Gradle Plugin User Guide - Android Studio Project Sitehttp://tools.android.com/tech-docs/new-build-s ...

  4. 【Java EE 学习 19】【使用过滤器实现全站压缩】【使用ThreadLocal模式解决跨DAO事务回滚问题】

    一.使用过滤器实现全站压缩 1.目标:对网站的所有JSP页面进行页面压缩,减少用户流量的使用.但是对图片和视频不进行压缩,因为图片和视频的压缩率很小,而且处理所需要的服务器资源很大. 2.实现原理: ...

  5. JAVA中this用法小结

    Java关键字this只能用于方法方法体内.当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this.因此,this只能在类中的非静态方法中使用,静 ...

  6. 关于Application Insights遥测功能使用【遇到问题】

    简介:Application Insights是微软发布的一个在线服务,可以监测自己的网站应用,进行性能管理以及使用分析. Application Insights功能一开始是出现在Visualstu ...

  7. WPF线程(Step1)——Dispatcher

    使用WPF开发时经常会遇上自己建立的线程需要更新界面UI内容,从而导致的跨线程问题. 异常内容: 异常类型:System.InvalidOperationException 异常描述: "S ...

  8. LeetCode——Rotate Image(二维数组顺时针旋转90度)

      问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

  9. 以管理身份运行cmd

    搜索出cmd程序,然后右键-以管理员身份运行

  10. Arduino101学习笔记(六)—— 高级IO

    1.位移输出函数(8位) 输入value数据后Arduino会自动把数据移动分配到8个并行输出端. 其中dataPin为连接DS的引脚号, clockPin为连接SH_CP的引脚号, bitOrder ...