{"record":{"id":"7d0d79df173954f3","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-refresh","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Refresh\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringAddOrEdit, \"Refresh\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs","lineNumber":301,"sourceCode":"        }\n\n        /// <summary>\n        /// The top-level groups, constructed according to the descriptions\n        /// given in GroupDescriptions.\n        /// </summary>\n        public virtual ReadOnlyObservableCollection<object> Groups\n        {\n            get { return null; }\n        }\n\n        /// <summary>\n        /// Re-create the view, using any <seealso cref=\"SortDescriptions\"/> and/or <seealso cref=\"Filter\"/>.\n        /// </summary>\n        public virtual void Refresh()\n        {\n            IEditableCollectionView ecv = this as IEditableCollectionView;\n            if (ecv != null && (ecv.IsAddingNew || ecv.IsEditingItem))\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Refresh\"));\n\n            RefreshInternal();\n        }\n\n        internal void RefreshInternal()\n        {\n            if (AllowsCrossThreadChanges)\n                VerifyAccess();\n\n            RefreshOverride();\n\n            SetFlag(CollectionViewFlags.NeedsRefresh, false);\n        }\n\n        /// <summary>\n        /// Enter a Defer Cycle.\n        /// Defer cycles are used to coalesce changes to the ICollectionView.\n        /// </summary>","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs#L283-L319","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nif (ecv.IsAddingNew) ecv.CommitNew();\necv.EditItem(item);\ncollectionView.Refresh(); // InvalidOperationException\n// after\nif (ecv.IsEditingItem) ecv.CommitEdit();\nif (ecv.IsAddingNew) ecv.CommitNew();\ncollectionView.Refresh();","handlingStrategy":"validation","validationCode":"var ecv = (IEditableCollectionView)view;\nif (ecv.IsAddingNew) ecv.CommitNew();\nif (ecv.IsEditingItem) ecv.CommitEdit();\nview.Refresh();","typeGuard":"bool canRefresh = view is IEditableCollectionView ecv && !ecv.IsAddingNew && !ecv.IsEditingItem;","tryCatchPattern":"try { view.Refresh(); }\ncatch (InvalidOperationException) { ecv.CommitEdit(); ecv.CommitNew(); view.Refresh(); }","preventionTips":["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"],"tags":["wpf","collectionview","invalid-operation","editing-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}