dotnet/wpf · error · ArgumentException
SR.SelectedCellsCollection_InvalidItem
Error message
SR.SelectedCellsCollection_InvalidItem
What it means
DataGrid.SelectedCellsCollection.Add throws ArgumentException (SelectedCellsCollection_InvalidItem) when the DataGridCellInfo passed does not belong to the DataGrid's items/columns — IsValidPublicCell checks that the cell references a valid item and column of the owning DataGrid.
Solutions
- Build DataGridCellInfo only from (item, column) pairs that exist in the target DataGrid's Items and Columns collections.
- Re-resolve saved selections against the current grid after rebinding instead of reusing stale DataGridCellInfo objects.
- Avoid sharing cell info objects across DataGrid instances.
Example fix
// before cell = new DataGridCellInfo(otherGridItem, column); dg.SelectedCells.Add(cell); // after var item = dg.Items.Contains(otherGridItem) ? otherGridItem : null; var col = dg.Columns.Contains(column) ? column : null; if (item != null && col != null) dg.SelectedCells.Add(new DataGridCellInfo(item, col));
Defensive patterns
Strategy: validation
Validate before calling
if (dg.Items.Contains(item) && dg.Columns.Contains(column))
dg.SelectedCells.Add(new DataGridCellInfo(item, column)); Type guard
bool IsValidCell(DataGridCellInfo c, DataGrid dg) => dg.Items.Contains(c.Item) && (c.Column == null || dg.Columns.Contains(c.Column));
Try / catch
try { dg.SelectedCells.Add(cell); } catch (ArgumentException) { /* cell not in this grid; skip */ } Prevention
- Only build DataGridCellInfo from the target grid's own Items and Columns
- Re-resolve persisted selections after rebinding
- Never share cell info across DataGrid instances
When it happens
Trigger: Calling dataGrid.SelectedCells.Add(cellInfo) with a DataGridCellInfo constructed from an item not in the grid's Items, a column not in its Columns, or a cellInfo from another DataGrid instance.
Common situations: Programmatically restoring selection state persisted from another grid or session, or building DataGridCellInfo manually with mismatched item/column references after the grid was re-bound.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- ArgumentOutOfRangeException (timeout was Duration.Automatic)
- Can only call SelectAll when CanSelectMultipleItems is true.
- Collection_BadRank
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_ArrayCannotBeMultidimensional
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e12188af32018e75.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs:69
}
#endregion
#region IList<T> Members
/// <summary>
/// Adds a cell to the list.
/// </summary>
/// <param name="cell">The cell to add.</param>
public void Add(DataGridCellInfo cell)
{
_owner.Dispatcher.VerifyAccess();
ValidateIsReadOnly();
if (!IsValidPublicCell(cell))
{
throw new ArgumentException(SR.SelectedCellsCollection_InvalidItem, nameof(cell));
}
if (Contains(cell))
{
throw new ArgumentException(SR.SelectedCellsCollection_DuplicateItem, nameof(cell));
}
AddValidatedCell(cell);
}
/// <summary>
/// Adds a validated cell to the list.
/// </summary>
/// <param name="cell">The cell to add.</param>
internal void AddValidatedCell(DataGridCellInfo cell)
{
Debug.Assert(IsValidCell(cell), "The cell should be valid.");
Debug.Assert(!Contains(cell), "VirtualizedCellInfoCollection does not support duplicate items.");View on GitHub (pinned to 81131a70a4)