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
- Call CommitNew()/CancelNew() (IEditableCollectionView) to close any AddNew transaction before Refresh
- Call CommitEdit()/CancelEdit() to close any pending EditItem transaction
- Defer the Refresh until the add/edit completes, e.g. handle the transaction end event then refresh
- 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
- Always pair AddNew/EditItem with Commit/Cancel on the same view instance
- Guard shared refresh helpers with IsAddingNew/IsEditingItem checks
- Drive DataGrid edit commits (CommitEdit) before programmatic view changes
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
- SR.CurrentChangingCannotBeCanceled
- SR.Format(SR.MemberNotAllowedDuringAddOrEdit…
- SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Remove")
- SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "RemoveAt")
- Animation_Invalid_DefaultValue
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)