dotnet/wpf · error · InvalidOperationException

SR.DataGridRow_CannotSelectRowWhenCells

Error message

SR.DataGridRow_CannotSelectRowWhenCells

What it means

ISelectionItemProvider.AddToSelection on a DataGrid row peer throws InvalidOperationException when the DataGrid's SelectionUnit is not FullRow (e.g. Cell or CellOrRowHeader). In cell-selection mode rows cannot be programmatically added to selection via the row peer.

Solutions

  1. Set DataGrid.SelectionUnit to FullRow if row selection is required.
  2. Select the specific cells instead of the row when SelectionUnit is cell-based.
  3. Use the DataGrid's SelectedCells API directly instead of the row selection pattern.
  4. Catch InvalidOperationException and branch on IsRowSelectionUnit before selecting.

Example fix

// before
selectionItemPattern.AddToSelection(); // cell selection mode
// after
if (dataGrid.SelectionUnit == DataGridSelectionUnit.FullRow)
    selectionItemPattern.AddToSelection();
else
    dataGrid.SelectedCells.Add(new DataGridCellInfo(item, dataGrid.Columns[0]));
Defensive patterns

Strategy: validation

Validate before calling

if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
    throw new SkipRowSelectionException(); // or select cells instead

Try / catch

try { selectionItemPattern.AddToSelection(); }
catch (InvalidOperationException) { dataGrid.SelectedCells.Add(new DataGridCellInfo(item, dataGrid.Columns[0])); }

Prevention

When it happens

Trigger: Calling AddToSelection via UIA on a DataGridRowAutomationPeer whose OwningDataGrid.SelectionUnit is DataGridSelectionUnit.Cell or CellOrRowHeader.

Common situations: UIA test suites written against default SelectionUnit (FullRow) then run against a grid configured with cell selection; accessibility clients attempting row selection in cell-mode grids.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridItemAutomationPeer.cs:288

            get
            {
                return this.OwningDataGrid.SelectedItems.Contains(Item);
            }
        }

        IRawElementProviderSimple ISelectionItemProvider.SelectionContainer
        {
            get
            {
                return ProviderFromPeer(_dataGridAutomationPeer);
            }
        }

        void ISelectionItemProvider.AddToSelection()
        {
            if (!IsRowSelectionUnit)
            {
                throw new InvalidOperationException(SR.DataGridRow_CannotSelectRowWhenCells);
            }

            // If item is already selected - do nothing
            object item = Item;
            if (this.OwningDataGrid.SelectedItems.Contains(item))
            {
                return;
            }

            EnsureEnabled();

            if (this.OwningDataGrid.SelectionMode == DataGridSelectionMode.Single &&
                this.OwningDataGrid.SelectedItems.Count > 0)
            {
                throw new InvalidOperationException();
            }

            if (this.OwningDataGrid.Items.Contains(item))

View on GitHub (pinned to 81131a70a4)