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.
Tags: DataGrid, IEditableObject, WPF






In the WPF Datagrid that was released with .net4.0 there is an easy way to commit the complete row on every cell edit regardless of what data is bound to it. Check out the code here: http://codefluff.blogspot.com/2010/05/commiting-bound-cell-changes.html
@gauss, nice one. Thanks for the link.
Same problem as navigato. Any help on that?
@rgames, you can find an answer to navigato’s question here:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/c9a80fe6-cfca-4f5f-81d0-76c1ea3afa51
Basically, if you are not binding to a DataTable, you need to cast to your own type.
Hi Colin,
my “rowdata” is not derived from DataRowView Class and for that reason I always get “null” for rowView out of
DataRowView rowView = e.Row.Item as DataRowView;
And I’m not the only one with this problem as you can see here:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/c9a80fe6-cfca-4f5f-81d0-76c1ea3afa51
Do you think there is a possibility to get a solution as simple as your one for the problem? Would be great to get an answer.
Michael
Dude thanks a lot. You are a life saver, I was facing the same problem as Simon, but now all is fine Thank you!!!
Glad to hear that it helped you
Colin E.
Wow, you absolute legend! I had been struggling with this problem for days. I just couldn’t get understand why the changes made in my DataGrid were not being saved. Thank You! Thank You!
Thanks, Colin, you just helped me fix something that had been giving me fits for a while. Nice work!
unfortunately this interferes with the NewRowPlaceholder functionality
when EndEdit gets called in a New Row I don’t get a new placeholder
any suggestions?