dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

ListCollectionView.GroupBySelector cannot be changed while an AddNew or EditItem transaction is open. Assigning it mid-transaction throws InvalidOperationException with the resource string MemberNotAllowedDuringAddOrEdit("Grouping").

Solutions

  1. Call CommitNew/CommitEdit (or CancelNew/CancelEdit) before assigning GroupBySelector
  2. Guard the assignment with checks of IsAddingNew and IsEditingItem (cast to IEditableCollectionView if needed)
  3. Reapply grouping changes after the transaction completes

Example fix

// before
view.GroupBySelector = selector; // throws during EditItem
// after
var ecv = (IEditableCollectionView)view;
if (ecv.IsEditingItem) ecv.CommitEdit();
if (ecv.IsAddingNew) ecv.CommitNew();
view.GroupBySelector = selector;
Defensive patterns

Strategy: validation

Validate before calling

var ecv = collectionView as IEditableCollectionView;
if (!(collectionView.IsAddingNew || (ecv != null && ecv.IsEditingItem)))
    view.GroupBySelector = selector;

Try / catch

try { view.GroupBySelector = selector; }
catch (InvalidOperationException)
{
    // defer grouping change until the transaction ends
}

Prevention

When it happens

Trigger: Setting GroupBySelector after AddNew() or EditItem() without committing/cancelling the transaction first (the CanGroup check at line 481 passed, so grouping is enabled).

Common situations: A grouping-UI control (e.g. a group-by dropdown) updating the selector while a DataGrid row is being added or edited; programmatic reconfiguration of grouping during an active transaction.

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/04100b249be6d39a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs:483

                RefreshOrDefer();
            }
        }

        /// <summary>
        /// A delegate to select the group description as a function of the
        /// parent group and its level.
        /// </summary>
        [DefaultValue(null)]
        public virtual GroupDescriptionSelectorCallback GroupBySelector
        {
            get { return _group.GroupBySelector; }
            set
            {
                if (!CanGroup)
                    throw new NotSupportedException();
                if (IsAddingNew || IsEditingItem)
                    throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Grouping"));

                _group.GroupBySelector = value;

                RefreshOrDefer();
            }
        }

        /// <summary>
        /// Return the estimated number of records (or -1, meaning "don't know").
        /// </summary>
        public override int Count
        {
            get
            {
                VerifyRefreshNotDeferred();

                return InternalCount;
            }

View on GitHub (pinned to 81131a70a4)