dotnet/wpf · error · ArgumentException

SR.SelectedCellsCollection_DuplicateItem

Error message

SR.SelectedCellsCollection_DuplicateItem

What it means

DataGrid.SelectedCellsCollection.Add throws ArgumentException (SelectedCellsCollection_DuplicateItem) when the DataGridCellInfo being added is already present in the collection. The selected-cells collection is a set and rejects duplicates even though DataGridCellInfo is a lightweight value-like wrapper.

Solutions

  1. Check SelectedCells.Contains(cellInfo) before calling Add.
  2. Call SelectedCells.Clear() before bulk-restoring a selection snapshot.
  3. Use SelectAll or DeSelectAll helpers where they express intent better than per-cell adds.

Example fix

// before
foreach (var cell in savedCells) dg.SelectedCells.Add(cell);
// after
dg.SelectedCells.Clear();
foreach (var cell in savedCells) dg.SelectedCells.Add(cell);
Defensive patterns

Strategy: validation

Validate before calling

if (!dg.SelectedCells.Contains(cell)) dg.SelectedCells.Add(cell);

Try / catch

try { dg.SelectedCells.Add(cell); } catch (ArgumentException) { /* already selected; ignore */ }

Prevention

When it happens

Trigger: Calling SelectedCells.Add twice with equivalent cells — e.g. the same (item, column) pair, or cell info from a row already selected — without checking Contains first, often in bulk-restore selection loops.

Common situations: Re-applying saved selection state on top of existing selection, or handling SelectionChanged/selection-restore logic that fires multiple times for the same cell.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs:74

        /// <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.");

            int columnIndex;
            int rowIndex;
            ConvertCellInfoToIndexes(cell, out rowIndex, out columnIndex);
            AddRegion(rowIndex, columnIndex, 1, 1);

View on GitHub (pinned to 81131a70a4)