{"record":{"id":"7dd65cc71188ca95","repo":"dotnet/wpf","slug":"sr-virtualizedcellinfocollection-doesnotsupportindexchanges","errorCode":null,"errorMessage":"SR.VirtualizedCellInfoCollection_DoesNotSupportIndexChanges","messagePattern":"SR\\.VirtualizedCellInfoCollection_DoesNotSupportIndexChanges","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs","lineNumber":331,"sourceCode":"            {\n                CellRegion region = _regions[i];\n                if (region.Contains(columnIndex, rowIndex))\n                {\n                    return regionCount + (((rowIndex - region.Top) * region.Width) + columnIndex - region.Left);\n                }\n\n                regionCount += region.Size;\n            }\n\n            return -1;\n        }\n\n        /// <summary>\n        ///     Not supported.\n        /// </summary>\n        public void Insert(int index, DataGridCellInfo cell)\n        {\n            throw new NotSupportedException(SR.VirtualizedCellInfoCollection_DoesNotSupportIndexChanges);\n        }\n\n        /// <summary>\n        ///     Remove the cell from the collection.\n        /// </summary>\n        /// <param name=\"cell\">The cell to remove.</param>\n        /// <returns>true if the cell was removed. false otherwise.</returns>\n        public bool Remove(DataGridCellInfo cell)\n        {\n            _owner.Dispatcher.VerifyAccess();\n            ValidateIsReadOnly();\n\n            if (!IsEmpty)\n            {\n                int columnIndex;\n                int rowIndex;\n                ConvertCellInfoToIndexes(cell, out rowIndex, out columnIndex);\n","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs#L313-L349","documentation":"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.","triggerScenarios":"Calling SelectedCells.Insert(index, cell) directly, or calling it via a generic IList<DataGridCellInfo> / ICollection wrapper that routes to Insert.","commonSituations":"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.","solutions":["Use SelectedCells.Add(cell) instead of Insert — order is maintained by the grid, not the caller.","Clear selection and re-add desired cells via SelectedCells.Clear()/Add if building a selection from scratch.","Guard with collection.IsReadOnly / fixed known NotSupportedException before calling Insert.","Wrap in try-catch (NotSupportedException) if the input is untrusted."],"exampleFix":"// before\n((IList<DataGridCellInfo>)dataGrid.SelectedCells).Insert(0, cellInfo);\n// after\ndataGrid.SelectedCells.Add(cellInfo);","handlingStrategy":"validation","validationCode":"if (collection.IsReadOnly) throw new InvalidOperationException(\"Insert is not supported; use Add\");","typeGuard":null,"tryCatchPattern":"try { list.Insert(i, cell); } catch (NotSupportedException) { list.Add(cell); }","preventionTips":["Treat SelectedCells as an unordered virtualized view","Use Add/Clear/Remove, never positional operations"],"tags":["wpf","datagrid","notsupported","selection"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}