{"record":{"id":"b8bc518d6bb4a857","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringtransaction-cancelnew-b8bc51","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringTransaction, \"CancelNew\", \"EditItem\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringTransaction, \"CancelNew\", \"EditItem\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":941,"sourceCode":"            _group.RemoveSpecialItem(index, newItem, false /*loading*/);\n\n            // now pretend it just got added to the collection.  This will add it\n            // to the internal list with sort/filter, and to the groups\n            ProcessCollectionChanged(\n                    new NotifyCollectionChangedEventArgs(\n                                NotifyCollectionChangedAction.Add,\n                                newItem,\n                                newItemIndex));\n        }\n\n        /// <summary>\n        /// Complete the transaction started by <seealso cref=\"AddNew\"/>.  The new\n        /// item is removed from the collection.\n        /// </summary>\n        public void CancelNew()\n        {\n            if (IsEditingItem)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, \"CancelNew\", \"EditItem\"));\n            VerifyRefreshNotDeferred();\n\n            if (_newItem == NoNewItem)\n                return;\n\n            // remove the new item from the underlying collection.  Normally the\n            // collection will raise a Remove event, which we'll handle by calling\n            // EndNew to leave AddNew mode.\n            BindingOperations.AccessCollection(SourceList,\n                () =>\n                {\n                    ProcessPendingChanges();\n                    SourceList.RemoveAt(_newItemIndex);\n\n                    // if the collection doesn't raise events, do the work explicitly on its behalf\n                    if (_newItem != NoNewItem)\n                    {\n                        int index = AdjustBefore(NotifyCollectionChangedAction.Remove, _newItem, _newItemIndex);","sourceCodeStart":923,"sourceCodeEnd":959,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L923-L959","documentation":"CancelNew aborts an AddNew transaction, so it cannot run while an EditItem transaction is open. If IsEditingItem is true, ListCollectionView.CancelNew throws InvalidOperationException naming CancelNew and EditItem.","triggerScenarios":"Calling CancelNew() while an EditItem transaction is active — e.g. user pressed Escape on a new row while a cell-edit transaction was still open, or code called EditItem after AddNew and then tried CancelNew.","commonSituations":"DataGrid cancel-row-edit handlers racing with cell-level edit transactions; MVVM cancel commands that don't check which transaction is currently open.","solutions":["Call CommitEdit() or CancelEdit() to close the EditItem transaction before calling CancelNew","Check IsEditingItem/IsAddingNew before cancelling, and route to CancelEdit vs CancelNew accordingly","Ensure the UI commits or cancels the cell edit before cancelling the new-row transaction"],"exampleFix":"// before\ncollectionView.CancelNew(); // throws if EditItem is open\n// after\nvar ecv = (IEditableCollectionView)collectionView;\nif (ecv.IsEditingItem) ecv.CancelEdit();\nif (ecv.IsAddingNew) ecv.CancelNew();","handlingStrategy":"validation","validationCode":"var ecv = (IEditableCollectionView)view;\nif (ecv.IsEditingItem) ecv.CancelEdit();\nif (ecv.IsAddingNew) ecv.CancelNew();","typeGuard":null,"tryCatchPattern":"try { view.CancelNew(); }\ncatch (InvalidOperationException)\n{\n    // an EditItem transaction is open; cancel/commit it first\n}","preventionTips":["Cancel the innermost (cell-edit) transaction before cancelling the new-row transaction","Check IsEditingItem before calling CancelNew","Ensure Escape-key handlers call CancelEdit before CancelNew when both may be active"],"tags":["wpf","data-binding","invalidoperationexception","listcollectionview","transaction"],"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"}