{"record":{"id":"1e99f0477ac8bad6","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-grouping","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Grouping\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringAddOrEdit, \"Grouping\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":2308,"sourceCode":"        // For the Group to report collection changed\n        private void OnGroupChanged(object sender, NotifyCollectionChangedEventArgs e)\n        {\n            if (e.Action == NotifyCollectionChangedAction.Add)\n            {\n                AdjustCurrencyForAdd(e.NewStartingIndex);\n            }\n            else if (e.Action == NotifyCollectionChangedAction.Remove)\n            {\n                AdjustCurrencyForRemove(e.OldStartingIndex);\n            }\n            OnCollectionChanged(e);\n        }\n\n        // The GroupDescriptions collection changed\n        private void OnGroupByChanged(object sender, NotifyCollectionChangedEventArgs e)\n        {\n            if (IsAddingNew || IsEditingItem)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Grouping\"));\n\n            // This is a huge change.  Just refresh the view.\n            RefreshOrDefer();\n        }\n\n        // A group description for one of the subgroups changed\n        private void OnGroupDescriptionChanged(object sender, EventArgs e)\n        {\n            if (IsAddingNew || IsEditingItem)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Grouping\"));\n\n            // This is a huge change.  Just refresh the view.\n            RefreshOrDefer();\n        }\n\n        // An item was inserted into the collection.  Update the groups.\n        private void AddItemToGroups(object item)\n        {","sourceCodeStart":2290,"sourceCodeEnd":2326,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L2290-L2326","documentation":"BindingListCollectionView throws this InvalidOperationException from its OnGroupByChanged handler when the group descriptions change while a transactional edit is in progress (AddNew has not been committed or EditItem has not been ended). WPF disallows changing grouping during an Add/Edit transaction because the new group structure cannot be applied consistently to an item being added or edited. It is a deliberate state-machine guard, not a bug.","triggerScenarios":"Calling collectionView.AddNew() (or EditItem) and, before CommitNew/CommitEdit, modifying the GroupDescriptions collection (adding/removing a PropertyGroupDescription) or programmatically altering grouping via CollectionViewGroup changes, which fires OnGroupByChanged while IsAddingNew or IsEditingItem is true.","commonSituations":"Data-binding driven UI where a group-description change (e.g. user picks a new 'group by' option in a DataGrid/ListView toolbar) races with an uncommitted new-item row or active cell edit; editing code that adjusts grouping inside the CollectionView.AddNew editing template without calling CommitNew first.","solutions":["Commit or cancel the transaction before changing grouping: call CommitNew()/CommitEdit() (or CancelNew()/CancelEditItem) immediately before modifying GroupDescriptions.","Defer the grouping change until after the edit completes, e.g. queue it and apply it in a handler for the row commit event.","If grouping must not change during edits at all, disable the group-by UI while IsAddingNew/IsEditingItem is true on the view."],"exampleFix":"// before\ncollectionView.AddNew();\ncollectionView.GroupDescriptions.Add(new PropertyGroupDescription(\"Category\")); // throws\n\n// after\ncollectionView.CommitNew();\ncollectionView.GroupDescriptions.Add(new PropertyGroupDescription(\"Category\"));","handlingStrategy":"validation","validationCode":"if (!(collectionView.IsAddingNew || collectionView.IsEditingItem))\n{\n    collectionView.GroupDescriptions.Add(new PropertyGroupDescription(\"Category\"));\n}","typeGuard":null,"tryCatchPattern":"try { ChangeGrouping(); } catch (InvalidOperationException) { /* retry after CommitNew/CommitEdit */ }","preventionTips":["Always call CommitNew/CancelNew and CommitEdit/CancelEditItem before any grouping change","Disable group-by UI while IsAddingNew/IsEditingItem is true","Wrap grouping mutations in a helper that checks the transaction state first"],"tags":["wpf","data-binding","collection-view","grouping","invalid-state-transition"],"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"}