dotnet/wpf · error · InvalidOperationException
SR.DataGrid_CannotSelectCell
Error message
SR.DataGrid_CannotSelectCell
What it means
DataGridCellItemAutomationPeer's ISelectionItemProvider.AddToSelection can only add cells to selection when the DataGrid's SelectionUnit is Cell (or cell-level selection is meaningful). If the DataGrid is not in cell selection mode, the peer throws InvalidOperationException with SR.DataGrid_CannotSelectCell because full-row selection cannot accommodate cell-based selection requests.
Solutions
- Set DataGrid.SelectionUnit="Cell" when cell-level automation selection is required.
- Change the automation client to use row/row-item selection patterns when SelectionUnit is FullRow.
- Catch InvalidOperationException and fall back to selecting the row containing the cell.
- Check SelectionUnit before invoking selection APIs in test code.
Example fix
// before
cellSelectionItem.AddToSelection();
// after
if (dataGrid.SelectionUnit == DataGridSelectionUnit.Cell)
cellSelectionItem.AddToSelection();
else
rowSelectionItem.Select(); Defensive patterns
Strategy: validation
Validate before calling
bool canAdd = dataGrid.SelectionUnit == DataGridSelectionUnit.Cell;
Try / catch
try { peer.AddToSelection(); } catch (InvalidOperationException) { rowPeer.Select(); } Prevention
- Match automation scripts to DataGrid SelectionUnit
- Set SelectionUnit=Cell in test fixtures needing cell selection
- Fall back to row selection on failure
When it happens
Trigger: An automation client calls AddToSelection() on a DataGrid cell while DataGrid.SelectionUnit is FullRow (or the peer's IsCellSelectionUnit check fails for another reason such as a shared/placeholder item).
Common situations: UIA tests written against DataGrids configured with SelectionUnit="Cell" being run against one with SelectionUnit="FullRow" (default); theme or control-template changes; automated accessibility tooling selecting cells.
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
- Can only call SelectAll when CanSelectMultipleItems is true.
- SR.DataGrid_AutomationInvokeFailed
- SR.DataGrid_CannotSelectCell
- SR.DataGrid_ColumnIsReadOnly
- SR.DataGridColumnHeaderItemAutomationPeer_Unresizable
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a723d478dae811d8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridCellItemAutomationPeer.cs:687
get
{
return this.OwningDataGrid.SelectedCellsInternal.Contains(new DataGridCellInfo(Item, _column));
}
}
IRawElementProviderSimple ISelectionItemProvider.SelectionContainer
{
get
{
return this.ContainingGrid;
}
}
void ISelectionItemProvider.AddToSelection()
{
if (!IsCellSelectionUnit)
{
throw new InvalidOperationException(SR.DataGrid_CannotSelectCell);
}
// If item is already selected - do nothing
DataGridCellInfo currentCellInfo = new DataGridCellInfo(Item, _column);
if (this.OwningDataGrid.SelectedCellsInternal.Contains(currentCellInfo))
{
return;
}
EnsureEnabled();
if (this.OwningDataGrid.SelectionMode == DataGridSelectionMode.Single &&
this.OwningDataGrid.SelectedCells.Count > 0)
{
throw new InvalidOperationException();
}
this.OwningDataGrid.SelectedCellsInternal.Add(currentCellInfo);View on GitHub (pinned to 81131a70a4)