{"record":{"id":"fefd9db5fdd642cd","repo":"dotnet/wpf","slug":"sr-membernotallowedduringaddoredit-formatted-with-grouping","errorCode":null,"errorMessage":"SR.MemberNotAllowedDuringAddOrEdit (formatted with \"Grouping\")","messagePattern":"SR\\.MemberNotAllowedDuringAddOrEdit \\(formatted with \"Grouping\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":2999,"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, LiveShapingItem lsi)\n        {","sourceCodeStart":2981,"sourceCodeEnd":3017,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L2981-L3017","documentation":"ListCollectionView throws InvalidOperationException when the GroupDescriptions collection raises its change event (OnGroupByChanged) while an AddNew or EditItem transaction is active. Grouping changes trigger a refresh which cannot happen mid-transaction. Complete or cancel the transaction first.","triggerScenarios":"Adding/removing a GroupDescription (view.GroupDescriptions.Add(...)) while IsAddingNew or IsEditingItem is true; PropertyChanged on an existing GroupDescription fires during an open add/edit transaction.","commonSituations":"User regroups a list (e.g. changes a DataGrid grouping) while a new item is being added; programmatic grouping applied in a CollectionChanged handler during an edit; async UI updates changing grouping while a row editor is open.","solutions":["Commit or cancel the add/edit transaction before changing GroupDescriptions","Guard grouping changes with checks on IsAddingNew/IsEditingItem and defer them","Restructure code so grouping is configured once before any item transactions begin"],"exampleFix":"// before\nview.GroupDescriptions.Add(new PropertyGroupDescription(\"Category\"));\n// after\nif (view.IsAddingNew) view.CancelNew();\nif (view.IsEditingItem) view.CancelEdit();\nview.GroupDescriptions.Add(new PropertyGroupDescription(\"Category\"));","handlingStrategy":"validation","validationCode":"if (!view.IsAddingNew && !view.IsEditingItem) view.GroupDescriptions.Add(new PropertyGroupDescription(\"Category\"));","typeGuard":null,"tryCatchPattern":"try { view.GroupDescriptions.Add(gd); } catch (InvalidOperationException) { /* defer until transaction closes */ }","preventionTips":["Guard GroupDescriptions changes with transaction-state checks","Avoid regrouping from CollectionChanged handlers that can fire mid-edit","Configure grouping before starting item transactions"],"tags":["wpf","databinding","collectionview","grouping"],"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"}