dotnet/wpf · error · NotSupportedException

SR.VirtualizedCellInfoCollection_DoesNotSupportIndexChanges

Error message

SR.VirtualizedCellInfoCollection_DoesNotSupportIndexChanges

What it means

DataGrid's selected-cells collection (VirtualizedCellInfoCollection) does not support Insert because it is a computed, virtualized view of selection state, not a stored list. Any attempt to insert at an index throws NotSupportedException.

Solutions

  1. Use SelectedCells.Add(cell) instead of Insert — order is maintained by the grid, not the caller.
  2. Clear selection and re-add desired cells via SelectedCells.Clear()/Add if building a selection from scratch.
  3. Guard with collection.IsReadOnly / fixed known NotSupportedException before calling Insert.
  4. Wrap in try-catch (NotSupportedException) if the input is untrusted.

Example fix

// before
((IList<DataGridCellInfo>)dataGrid.SelectedCells).Insert(0, cellInfo);
// after
dataGrid.SelectedCells.Add(cellInfo);
Defensive patterns

Strategy: validation

Validate before calling

if (collection.IsReadOnly) throw new InvalidOperationException("Insert is not supported; use Add");

Try / catch

try { list.Insert(i, cell); } catch (NotSupportedException) { list.Add(cell); }

Prevention

When it happens

Trigger: Calling SelectedCells.Insert(index, cell) directly, or calling it via a generic IList<DataGridCellInfo> / ICollection wrapper that routes to Insert.

Common situations: Code written against IList<T> patterns that inserts cells positionally; porting code from ObservableCollection-based selections; serialization helpers that try to rebuild the collection item-by-item.

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

Appendix: source

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

            {
                CellRegion region = _regions[i];
                if (region.Contains(columnIndex, rowIndex))
                {
                    return regionCount + (((rowIndex - region.Top) * region.Width) + columnIndex - region.Left);
                }

                regionCount += region.Size;
            }

            return -1;
        }

        /// <summary>
        ///     Not supported.
        /// </summary>
        public void Insert(int index, DataGridCellInfo cell)
        {
            throw new NotSupportedException(SR.VirtualizedCellInfoCollection_DoesNotSupportIndexChanges);
        }

        /// <summary>
        ///     Remove the cell from the collection.
        /// </summary>
        /// <param name="cell">The cell to remove.</param>
        /// <returns>true if the cell was removed. false otherwise.</returns>
        public bool Remove(DataGridCellInfo cell)
        {
            _owner.Dispatcher.VerifyAccess();
            ValidateIsReadOnly();

            if (!IsEmpty)
            {
                int columnIndex;
                int rowIndex;
                ConvertCellInfoToIndexes(cell, out rowIndex, out columnIndex);

View on GitHub (pinned to 81131a70a4)