{"record":{"id":"ab774bf8ec66e1a6","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-sorting","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Sorting\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringAddOrEdit, \"Sorting\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":2169,"sourceCode":"            get { return ((_blv != null) && !String.IsNullOrEmpty(_customFilter)); }\n        }\n\n        // can the group name(s) for an item change after we've grouped the item?\n        private bool CanGroupNamesChange\n        {\n            // There's no way we can deduce this - the app has to tell us.\n            // If this is true, removing a grouped item is quite difficult.\n            // We cannot rely on its group names to tell us which group we inserted\n            // it into (they may have been different at insertion time), so we\n            // have to do a linear search.\n            get { return true; }\n        }\n\n        // SortDescription was added/removed, refresh CollView\n        private void SortDescriptionsChanged(object sender, NotifyCollectionChangedEventArgs e)\n        {\n            if (IsAddingNew || IsEditingItem)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Sorting\"));\n\n            RefreshOrDefer();\n        }\n\n        // convert from Avalon SortDescriptions to the corresponding .NET collection\n        private ListSortDescriptionCollection ConvertSortDescriptionCollection(SortDescriptionCollection sorts)\n        {\n            PropertyDescriptorCollection pdc;\n            ITypedList itl;\n            Type itemType;\n\n            if ((itl = InternalList as ITypedList) != null)\n            {\n                pdc = itl.GetItemProperties(null);\n            }\n            else if ((itemType = GetItemType(true)) != null)\n            {\n                pdc = TypeDescriptor.GetProperties(itemType);","sourceCodeStart":2151,"sourceCodeEnd":2187,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L2151-L2187","documentation":"SortDescriptionsChanged throws InvalidOperationException (SR.MemberNotAllowedDuringAddOrEdit, \"Sorting\") when the SortDescriptions collection changes while the view is in the middle of an AddNew or EditItem transaction. WPF forbids changing sorting during an pending add/edit because it would invalidate the transaction.","triggerScenarios":"Calling view.SortDescriptions.Add/Remove/Clear (or changing grouping) while IsAddingNew or IsEditingItem is true — i.e. between AddNew()/CommitNew or EditItem()/CommitEdit calls.","commonSituations":"UI code that re-sorts or re-applies sort descriptions (e.g. on a column-header click) while an edit or add-new is still pending because a previous CommitNew/CommitEdit was never invoked; event handlers updating sorting during data entry.","solutions":["Call CommitNew() and CommitEdit() before changing SortDescriptions.","Guard sorting changes: if (view.IsAddingNew) view.CommitNew(); if (view.IsEditingItem) view.CommitEdit(); then apply sorts.","Use CancelNew()/CancelEditItem appropriately to abort the transaction before sorting.","Defer the sort-description change until the transaction completes (e.g. queue it for after CommitEdit)."],"exampleFix":"// before\nview.SortDescriptions.Add(new SortDescription(\"Name\", ListSortDirection.Ascending)); // may throw mid-transaction\n\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.CommitNew();\nif (view.IsEditingItem) view.CommitEdit();\nview.SortDescriptions.Add(sortDescription);","typeGuard":"bool canChangeSorting(ICollectionView v) => !v.IsAddingNew && !((BindingListCollectionView)v).IsEditingItem;","tryCatchPattern":"try { view.SortDescriptions.Clear(); }\ncatch (InvalidOperationException) { view.CommitEdit(); view.SortDescriptions.Clear(); }","preventionTips":["Always call CommitNew/CommitEdit before changing sorting","Centralize sort changes in one helper that ends transactions first","Audit UI event handlers (column clicks) for pending add/edit transactions"],"tags":["wpf","data-binding","sorting","addnew","edititem","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-21T21:30:21.729Z"}