{"record":{"id":"d3b82e7058d481c9","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-remove-d3b82e","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Remove\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringAddOrEdit, \"Remove\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":1062,"sourceCode":"        /// The index is interpreted with respect to the view (not with respect to\n        /// the underlying collection).\n        /// </summary>\n        public void RemoveAt(int index)\n        {\n            if (IsEditingItem || IsAddingNew)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"RemoveAt\"));\n            VerifyRefreshNotDeferred();\n\n            RemoveImpl(GetItemAt(index), index);\n        }\n\n        /// <summary>\n        /// Remove the given item from the underlying collection.\n        /// </summary>\n        public void Remove(object item)\n        {\n            if (IsEditingItem || IsAddingNew)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Remove\"));\n            VerifyRefreshNotDeferred();\n\n            int index = InternalIndexOf(item);\n            if (index >= 0)\n            {\n                RemoveImpl(item, index);\n            }\n        }\n\n        private void RemoveImpl(object item, int index)\n        {\n            if (item == CollectionView.NewItemPlaceholder)\n                throw new InvalidOperationException(SR.RemovingPlaceholder);\n\n            BindingOperations.AccessCollection(SourceList,\n                () =>\n                {\n                    ProcessPendingChanges();","sourceCodeStart":1044,"sourceCodeEnd":1080,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L1044-L1080","documentation":"ListCollectionView.Remove(object) throws InvalidOperationException when the view has a pending AddNew or EditItem transaction. As with RemoveAt, removing an item is forbidden during an add/edit transaction to keep the pending item state consistent.","triggerScenarios":"Calling view.Remove(item) after AddNew() without CommitNew()/CancelNew(), or after EditItem(item) without CommitEdit()/CancelEdit().","commonSituations":"Programmatic deletes issued while a DataGrid row is in edit mode; a delete command bound to a button that remains enabled during add-new.","solutions":["End the transaction first: CommitNew()/CancelNew() when IsAddingNew, CommitEdit()/CancelEdit() when IsEditingItem, then call Remove.","Check view.IsAddingNew / view.IsEditingItem before removing and skip or queue the removal otherwise.","Use ICollectionView.Refresh or rebind after ending the transaction if the view state seems stale."],"exampleFix":"// before\ncollectionView.Remove(selectedItem);\n\n// after\nif (collectionView.IsAddingNew) collectionView.CancelNew();\nif (collectionView.IsEditingItem) collectionView.CancelEdit();\ncollectionView.Remove(selectedItem);","handlingStrategy":"validation","validationCode":"if (view.IsAddingNew) view.CommitNew();\nif (view.IsEditingItem) view.CommitEdit();\n// now safe:\nview.Remove(item);","typeGuard":"bool CanRemove(ICollectionView v, object item) => !v.IsAddingNew && !v.IsEditingItem && !Equals(item, CollectionView.NewItemPlaceholder);","tryCatchPattern":"try { view.Remove(item); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"NotAllowedDuringAddOrEdit\"))\n{ /* commit/cancel then retry */ }","preventionTips":["Guard delete commands with IsAddingNew/IsEditingItem checks.","End transactions in response to focus-loss/edit-completion events.","Wrap Remove in a helper enforcing transaction closure."],"tags":["wpf","collectionview","invalid-operation"],"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-22T01:17:13.364Z"}