dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Refresh")

Error message

SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Refresh")

What it means

CollectionView.Refresh throws InvalidOperationException when an AddNew or EditItem transaction is still open on the same view (via IEditableCollectionView). Re-creating the view during an add/edit would discard the pending new/edited item, so WPF forbids it. Call EndEdit/CancelEdit or CommitNew/CancelNew first.

Solutions

  1. Call CommitNew()/CancelNew() (IEditableCollectionView) to close any AddNew transaction before Refresh
  2. Call CommitEdit()/CancelEdit() to close any pending EditItem transaction
  3. Defer the Refresh until the add/edit completes, e.g. handle the transaction end event then refresh
  4. Guard code that changes Filter/SortDescriptions with a check of IsAddingNew/IsEditingItem

Example fix

// before
if (ecv.IsAddingNew) ecv.CommitNew();
ecv.EditItem(item);
collectionView.Refresh(); // InvalidOperationException
// after
if (ecv.IsEditingItem) ecv.CommitEdit();
if (ecv.IsAddingNew) ecv.CommitNew();
collectionView.Refresh();
Defensive patterns

Strategy: validation

Validate before calling

var ecv = (IEditableCollectionView)view;
if (ecv.IsAddingNew) ecv.CommitNew();
if (ecv.IsEditingItem) ecv.CommitEdit();
view.Refresh();

Type guard

bool canRefresh = view is IEditableCollectionView ecv && !ecv.IsAddingNew && !ecv.IsEditingItem;

Try / catch

try { view.Refresh(); }
catch (InvalidOperationException) { ecv.CommitEdit(); ecv.CommitNew(); view.Refresh(); }

Prevention

When it happens

Trigger: Calling Refresh() (directly or indirectly via OnParametersChanged/OnSourceDataChanged, e.g. changing SortDescriptions or Filter) while IsAddingNew or IsEditingItem is true — i.e. between AddNew/EditItem and the matching Commit/Cancel call.

Common situations: Re-sorting or re-filtering a DataGrid's ItemsCollection from code while the user is editing a row or has clicked 'add new' and not committed; a parent Refresh triggered by a property change during an edit session.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7d0d79df173954f3. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs:301

        }

        /// <summary>
        /// The top-level groups, constructed according to the descriptions
        /// given in GroupDescriptions.
        /// </summary>
        public virtual ReadOnlyObservableCollection<object> Groups
        {
            get { return null; }
        }

        /// <summary>
        /// Re-create the view, using any <seealso cref="SortDescriptions"/> and/or <seealso cref="Filter"/>.
        /// </summary>
        public virtual void Refresh()
        {
            IEditableCollectionView ecv = this as IEditableCollectionView;
            if (ecv != null && (ecv.IsAddingNew || ecv.IsEditingItem))
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Refresh"));

            RefreshInternal();
        }

        internal void RefreshInternal()
        {
            if (AllowsCrossThreadChanges)
                VerifyAccess();

            RefreshOverride();

            SetFlag(CollectionViewFlags.NeedsRefresh, false);
        }

        /// <summary>
        /// Enter a Defer Cycle.
        /// Defer cycles are used to coalesce changes to the ICollectionView.
        /// </summary>

View on GitHub (pinned to 81131a70a4)