首页 > 代码库 > WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL
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.
WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL