{"record":{"id":"ab23d3e6d7774138","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-removeat-ab23d3","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/ListCollectionView.cs","lineNumber":1050,"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 && !SourceList.IsFixedSize; }\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":1032,"sourceCodeEnd":1068,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L1032-L1068","documentation":"ListCollectionView.RemoveAt throws InvalidOperationException when the view is in the middle of an AddNew or EditItem transaction. Removing items from the view is not allowed while a new item is being added or an existing one edited, because the remove would corrupt the pending transaction state.","triggerScenarios":"Calling view.RemoveAt(index) while IsAddingNew is true (after AddNew without CommitNew/CancelNew) or while IsEditingItem is true (after EditItem without CommitEdit/CancelEdit).","commonSituations":"DataGrid or ItemsControl editing sessions left open (user still editing a row) while code removes a row programmatically; batch-delete code running while an add-new row is pending in the UI.","solutions":["Commit or cancel the pending transaction first: call CommitNew()/CancelNew() if IsAddingNew, or CommitEdit()/CancelEdit() if IsEditingItem, before RemoveAt.","Guard the call: only call RemoveAt when !view.IsAddingNew && !view.IsEditingItem.","If the removal originates from UI events, defer it (Dispatcher.BeginInvoke) until after the edit transaction ends."],"exampleFix":"// before\ncollectionView.RemoveAt(index);\n\n// after\nif (collectionView.IsAddingNew) collectionView.CommitNew();\nif (collectionView.IsEditingItem) collectionView.CommitEdit();\ncollectionView.RemoveAt(index);","handlingStrategy":"validation","validationCode":"if (view.IsAddingNew) view.CommitNew();\nif (view.IsEditingItem) view.CommitEdit();\n// now safe:\nview.RemoveAt(index);","typeGuard":"bool CanMutate(ICollectionView v) => !v.IsAddingNew && !v.IsEditingItem;","tryCatchPattern":"try { view.RemoveAt(index); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"NotAllowedDuringAddOrEdit\"))\n{ /* end transaction and retry, or surface message */ }","preventionTips":["Always close AddNew/EditItem transactions before programmatic mutations.","Centralize view mutations in a helper that ends open transactions first.","Disable delete commands while IsAddingNew || IsEditingItem (e.g. via CanExecute)."],"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-21T21:30:21.729Z"}