Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid CellEditEnding - DataSet Not Updating Till Row Lost Focus

Tags:

wpf

datagrid

I need to be able to update values of a dataset once a cell loses focus from editing. I know when the cell loses focus (CellEditEnding), but problem is, the actual updating of it's context item does not occur till focus on that row actually occurs. This becomes a huge issue when there is only one item left, since it may never lose focus.

How do I make sure that each time a column edit is complete (CellEditEnding), the actual context for that row is updated at that point (not just when the row loses focus)

Thanks in advance!

like image 461
Anthony Greco Avatar asked Jun 05 '11 22:06

Anthony Greco


2 Answers

I met a similar problem, I have a DataGrid row that contains 5 columns. The data from the 5 columns will be updated in source only after the entire datagrid row has lost focus.

After some search, I found an easy way to do it. That is to add "UpdateSourceTrigger=LostFocus" in your databinding in the cell.

For example:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ComboBox DisplayMemberPath="Name"
                  ItemsSource="{Binding Path=MyDataSets}"
                  SelectedValue="{Binding Path=DataSelected, UpdateSourceTrigger=LostFocus}"/>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

This will do the trick, so when each of the cell lost focus, instead of the entire row, the data from the cell will update the source immediately.

like image 80
RainCast Avatar answered Oct 22 '22 19:10

RainCast


You can use DataGrid.CommitEdit from your DataGrid.CellEditEnding handler, being sure to handle reentrancy.

Here's a blog article that describes the technique:

  • Commiting bound cell changes immediately in WPF Datagrid
like image 10
Rick Sladkey Avatar answered Oct 22 '22 20:10

Rick Sladkey