twaldron
twaldron

Reputation: 2752

WPF DataGrid 'Refresh' is not allowed during an AddNew or EditItem transaction mvvm

I have the following grid

    <DataGrid

        x:Name="TablesDataGrid"
        Grid.Column="0"
        Grid.Row="1"
        ItemsSource="{Binding FilteredModels.View}"
        AlternationCount="2"
        AutoGenerateColumns="False"
        CanUserSortColumns="True"
        CanUserReorderColumns="False"
  CanUserDeleteRows="False"
  CanUserAddRows="False"
  SelectionMode="Extended"
        IsReadOnly="False"
  SelectionUnit="FullRow"
        RowHeight="25"
  HorizontalAlignment="Stretch"
  ColumnWidth="Auto">
            <DataGrid.Columns >
                <DataGridCheckBoxColumn Width="*" Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  IsReadOnly="False">
                    <DataGridCheckBoxColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.CheckAll}"/>
                        </DataTemplate>
                    </DataGridCheckBoxColumn.HeaderTemplate>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn Header="Source Table" Binding="{Binding SourceTableFullName}" Width="4*"></DataGridTextColumn>
                <DataGridTextColumn Header="EDW Schema"  Binding="{Binding SchemaName}" Width="2*"></DataGridTextColumn>
                <DataGridTextColumn Header="EDW Table" Binding="{Binding TableName}" Width="4*"></DataGridTextColumn>
                <DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

and then i have a seachCommand with performs the search on the collectionViewSource FilteredModels in the viewmodel and then calls

this.FilteredModels.View.Refresh();

when a user checks a few of the checkboxes and sends the grid into editmode and then performs a search we get the following error

WPF DataGrid 'Refresh' is not allowed during an AddNew or EditItem transaction

is there a way to force the grid out of edit mode when a check box is checked or maybe even when the seach button is clicked or some other fix for this?

thanks!

Upvotes: 16

Views: 21498

Answers (4)

BirukTes
BirukTes

Reputation: 45

Here is my solution, adapt it to your need.

private void CommitEditRefreshViewSafely()
{
    ((IEditableCollectionView)MyCollectionViewList.View).CommitEdit();
    MyCollectionViewList.View.Refresh();
}

Upvotes: 0

wondra
wondra

Reputation: 3583

There is a clean MVVM solution to the problem. First off, your ViewModels must implement IEditableObject interface (no-op should be enough). That, however, is not enough since the DataGrid will not listen to IEditableObject.CancelEdit.
Another problem is, that neither ICollectionView nor IEditableCollectionView implement the other one. While only ICollectionView can refresh, only IEditableCollectionView can commit/cancel. Luckily collection view returned by CollectionViewSource.GetDefaultView implements both:

// ViewModel.cs
public class ItemVM : IEditableObject, INotifyPropertyChanged { }

public class ModuleVM : INotifyPropertyChanged {
   ICollectionView Items { get; }

   public ModuleVM(ObservableCollection<ItemVM> items) {
      Items = CollectionViewSource.GetDefaultView(items);
   }

   public void RefreshSafely() {
      ((IEditableCollectionView)Items).CancelEdit(); // alterantively, CommitEdit()
      Items.Refresh();
   }
}

Or in other words, you can cast ICollectionView to IEditableCollectionView and call CancelEdit() first.

Upvotes: 6

Saad Abdullah
Saad Abdullah

Reputation: 2432

I know its too late to answer...but for someone who is looking for answer

use cancelEdit or commitEdit method two times in a sequence like this

//for commit

this.datagrid_layers.CommitEdit();
this.datagrid_layers.CommitEdit();

//for cancel

this.datagrid_layers.CancelEdit();
this.datagrid_layers.CancelEdit();

Upvotes: 51

jfin3204
jfin3204

Reputation: 709

you should be able to cast the selected item to IEditableObject and call EndEdit on it, or call the grids CancelEdit method.

Upvotes: 5

Related Questions