{"record":{"id":"ea8eb23a986c41cb","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-removeat","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"RemoveAt\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringAddOrEdit, \"RemoveAt\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":872,"sourceCode":"\n        /// <summary>\n        /// Return true if the view supports <seealso cref=\"Remove\"/> and\n        /// <seealso cref=\"RemoveAt\"/>.\n        /// </summary>\n        public bool CanRemove\n        {\n            get { return !IsEditingItem && !IsAddingNew && InternalList.AllowRemove; }\n        }\n\n        /// <summary>\n        /// Remove the item at the given index from the underlying collection.\n        /// 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);","sourceCodeStart":854,"sourceCodeEnd":890,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L854-L890","documentation":"RemoveAt(int) removes an item from the underlying collection via the view, but it is rejected with InvalidOperationException while an AddNew or EditItem transaction is in progress. Mutating the collection during an open transaction would corrupt the view's tracking of the new/edited item.","triggerScenarios":"Calling RemoveAt(index) when IsEditingItem or IsAddingNew is true — e.g. removing the currently-being-added or currently-being-edited row programmatically.","commonSituations":"A delete button handler that does not check for open add/edit transactions; DataGrid delete key pressed while a new row or cell edit is open; automation/scripts removing rows during batch edits.","solutions":["Call CommitEdit()/CancelEdit() and CommitNew()/CancelNew() before RemoveAt.","Guard with !view.IsEditingItem && !view.IsAddingNew before removing.","Cancel the in-flight transaction explicitly when the item being removed is the new/edited one.","Use CollectionView's CanRemove check (base CollectionView.CanRemove) before calling RemoveAt."],"exampleFix":"// before\nview.RemoveAt(index);\n\n// after\nif (!view.IsEditingItem && !view.IsAddingNew && view.CanRemove)\n    view.RemoveAt(index);\nelse\n    view.CancelEdit(); view.CancelNew(); // close transactions, then retry","handlingStrategy":"validation","validationCode":"// C#\nif (!view.IsEditingItem && !view.IsAddingNew && view.CanRemove)\n    view.RemoveAt(index);","typeGuard":"bool CanRemoveAt(IEditableCollectionView v) => !v.IsEditingItem && !v.IsAddingNew;","tryCatchPattern":"try { view.RemoveAt(index); }\ncatch (InvalidOperationException ex) { Log.Warn(\"RemoveAt during add/edit\", ex); view.CancelEdit(); view.CancelNew(); }","preventionTips":["Close add/edit transactions before any removal.","Check CanRemove before removing.","Disable delete UI while IsAddingNew/IsEditingItem."],"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-22T01:17:13.364Z"}