{"record":{"id":"fd78d407e3964500","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-remove","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/BindingListCollectionView.cs","lineNumber":884,"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(InternalList,\n                () =>\n                {\n                    ProcessPendingChanges();","sourceCodeStart":866,"sourceCodeEnd":902,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L866-L902","documentation":"Remove(object) deletes the given item from the underlying collection via the view, but throws InvalidOperationException if an AddNew or EditItem transaction is open. Same restriction as RemoveAt: the view will not mutate the collection while tracking a pending new/edited item.","triggerScenarios":"Calling Remove(item) when IsAddingNew or IsEditingItem is true, e.g. removing the item currently in edit mode.","commonSituations":"Context-menu 'Delete' invoked on a row that is actively being edited; batch-removal loops that don't close transactions between operations; automated tests that add items via AddNew and then call Remove.","solutions":["Close the open transaction (CommitEdit/CancelEdit, CommitNew/CancelNew) before calling Remove.","Check IsEditingItem and IsAddingNew (or CanRemove) before removing.","In DataGrid scenarios, call CommitEdit on the grid first so the view-level transaction is closed.","Batch operations: cancel/commit once before the loop rather than interleaving Remove with open transactions."],"exampleFix":"// before\nforeach (var item in itemsToRemove) view.Remove(item);\n\n// after\nif (view.IsEditingItem) view.CancelEdit();\nif (view.IsAddingNew) view.CancelNew();\nforeach (var item in itemsToRemove) view.Remove(item);","handlingStrategy":"validation","validationCode":"// C#\nif (!view.IsEditingItem && !view.IsAddingNew && view.CanRemove)\n    view.Remove(item);","typeGuard":"bool CanRemove(IEditableCollectionView v) => !v.IsEditingItem && !v.IsAddingNew;","tryCatchPattern":"try { view.Remove(item); }\ncatch (InvalidOperationException ex) { Log.Warn(\"Remove during add/edit\", ex); }","preventionTips":["Commit or cancel transactions before bulk removals.","Check CanRemove per removal.","Avoid interleaving Remove calls with open transactions in loops."],"tags":["wpf","data-binding","collection-mutation"],"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"}