dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Grouping")

Error message

SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Grouping")

What it means

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.

Solutions

  1. Commit or cancel the transaction before changing grouping: call CommitNew()/CommitEdit() (or CancelNew()/CancelEditItem) immediately before modifying GroupDescriptions.
  2. Defer the grouping change until after the edit completes, e.g. queue it and apply it in a handler for the row commit event.
  3. If grouping must not change during edits at all, disable the group-by UI while IsAddingNew/IsEditingItem is true on the view.

Example fix

// before
collectionView.AddNew();
collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Category")); // throws

// after
collectionView.CommitNew();
collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
Defensive patterns

Strategy: validation

Validate before calling

if (!(collectionView.IsAddingNew || collectionView.IsEditingItem))
{
    collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
}

Try / catch

try { ChangeGrouping(); } catch (InvalidOperationException) { /* retry after CommitNew/CommitEdit */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/1e99f0477ac8bad6. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs:2308

        // For the Group to report collection changed
        private void OnGroupChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                AdjustCurrencyForAdd(e.NewStartingIndex);
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                AdjustCurrencyForRemove(e.OldStartingIndex);
            }
            OnCollectionChanged(e);
        }

        // The GroupDescriptions collection changed
        private void OnGroupByChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (IsAddingNew || IsEditingItem)
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Grouping"));

            // This is a huge change.  Just refresh the view.
            RefreshOrDefer();
        }

        // A group description for one of the subgroups changed
        private void OnGroupDescriptionChanged(object sender, EventArgs e)
        {
            if (IsAddingNew || IsEditingItem)
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Grouping"));

            // This is a huge change.  Just refresh the view.
            RefreshOrDefer();
        }

        // An item was inserted into the collection.  Update the groups.
        private void AddItemToGroups(object item)
        {

View on GitHub (pinned to 81131a70a4)