{"record":{"id":"02a10ec3281f0e92","repo":"dotnet/wpf","slug":"sr-membernotallowedduringaddoredit-formatted-with-sorting","errorCode":null,"errorMessage":"SR.MemberNotAllowedDuringAddOrEdit (formatted with \"Sorting\")","messagePattern":"SR\\.MemberNotAllowedDuringAddOrEdit \\(formatted with \"Sorting\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":2906,"sourceCode":"            if (_sort != null)\n            {\n                ((INotifyCollectionChanged)_sort).CollectionChanged -= new NotifyCollectionChangedEventHandler(SortDescriptionsChanged);\n            }\n\n            _sort = descriptions;\n\n            if (_sort != null)\n            {\n                Invariant.Assert(_sort.Count == 0, \"must be empty SortDescription collection\");\n                ((INotifyCollectionChanged)_sort).CollectionChanged += new NotifyCollectionChangedEventHandler(SortDescriptionsChanged);\n            }\n        }\n\n        // SortDescription was added/removed, refresh CollectionView\n        private void SortDescriptionsChanged(object sender, NotifyCollectionChangedEventArgs e)\n        {\n            if (IsAddingNew || IsEditingItem)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Sorting\"));\n\n            // adding to SortDescriptions overrides custom sort\n            if (_sort.Count > 0)\n            {\n                _customSort = null;\n            }\n\n            RefreshOrDefer();\n        }\n\n        #region Grouping\n\n        // divide the data items into groups\n        private void PrepareGroups()\n        {\n            // if there's no grouping, there's nothing to do\n            if (!_isGrouping)\n                return;","sourceCodeStart":2888,"sourceCodeEnd":2924,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L2888-L2924","documentation":"ListCollectionView throws InvalidOperationException when SortDescriptions are added or removed while the collection is in the middle of an AddNew or EditItem transaction. Sorting operations would require a refresh, which is not allowed while a pending new/edited item exists. Finish or cancel the transaction before changing sorting.","triggerScenarios":"Calling SortDescriptions.Add/Remove/Clear (or ModifyProperties like ListCollectionView.SortDescriptions.Add(new SortDescription(...))) between AddNew/AddNewItem and CommitNew/CancelNew, or between EditItem and CommitEdit/CancelEdit.","commonSituations":"Data-binding UI where a user sorts a DataGrid column while a row is in add/edit mode; code that reacts to collection-changed events and adjusts sorting while an edit transaction is open; re-sorting in response to a property change during an ongoing AddNew.","solutions":["Commit or cancel the pending add/edit (CommitNew/CancelNew, CommitEdit/CancelEdit) before modifying SortDescriptions","Check IsAddingNew / IsEditingItem and defer sorting changes until the transaction completes","Subscribe to the view's transaction completion rather than applying sort changes immediately"],"exampleFix":"// before\nview.SortDescriptions.Add(new SortDescription(\"Name\", ListSortDirection.Ascending));\n// after\nif (view.IsAddingNew) view.CommitNew();\nif (view.IsEditingItem) view.CommitEdit();\nview.SortDescriptions.Add(new SortDescription(\"Name\", ListSortDirection.Ascending));","handlingStrategy":"validation","validationCode":"if (!view.IsAddingNew && !view.IsEditingItem) view.SortDescriptions.Add(new SortDescription(\"Name\", ListSortDirection.Ascending));","typeGuard":null,"tryCatchPattern":"try { view.SortDescriptions.Add(desc); } catch (InvalidOperationException) { /* transaction open; defer sort change */ }","preventionTips":["Check IsAddingNew/IsEditingItem before any view mutation","Commit or cancel add/edit transactions in UI event handlers promptly","Centralize sorting changes in one helper that guards transaction state"],"tags":["wpf","databinding","collectionview","invalid-state"],"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"}