{"record":{"id":"8515e1b44ec1a8db","repo":"dotnet/wpf","slug":"sr-virtualizedcellinfocollection-isreadonly","errorCode":null,"errorMessage":"SR.VirtualizedCellInfoCollection_IsReadOnly","messagePattern":"SR\\.VirtualizedCellInfoCollection_IsReadOnly","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs","lineNumber":1627,"sourceCode":"            if ((rowIndex >= 0) && (columnIndex >= 0) &&\n                (rowIndex < owner.Items.Count) && (columnIndex < owner.Columns.Count))\n            {\n                DataGridColumn column = owner.ColumnFromDisplayIndex(columnIndex);\n                ItemsControl.ItemInfo rowInfo = owner.ItemInfoFromIndex(rowIndex);\n\n                return CreateCellInfo(rowInfo, column, owner);\n            }\n            else\n            {\n                return DataGridCellInfo.Unset;\n            }\n        }\n\n        private void ValidateIsReadOnly()\n        {\n            if (IsReadOnly)\n            {\n                throw new NotSupportedException(SR.VirtualizedCellInfoCollection_IsReadOnly);\n            }\n        }\n\n        private void AddRegionToList(CellRegion region, List<DataGridCellInfo> list)\n        {\n            Debug.Assert(list != null, \"list should not be null.\");\n\n            for (int rowIndex = region.Top; rowIndex <= region.Bottom; rowIndex++)\n            {\n                ItemsControl.ItemInfo rowInfo = _owner.ItemInfoFromIndex(rowIndex);\n                for (int columnIndex = region.Left; columnIndex <= region.Right; columnIndex++)\n                {\n                    DataGridColumn column = _owner.ColumnFromDisplayIndex(columnIndex);\n                    DataGridCellInfo cellInfo = CreateCellInfo(rowInfo, column, _owner);\n                    list.Add(cellInfo);\n                }\n            }\n        }","sourceCodeStart":1609,"sourceCodeEnd":1645,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs#L1609-L1645","documentation":"ValidateIsReadOnly throws NotSupportedException when an attempt is made to modify the collection while IsReadOnly is true. The DataGrid selected-cells collection is read-only in most contexts (only the grid itself mutates it), so any mutating call on the public collection is rejected.","triggerScenarios":"Calling Add/Clear/Remove (or any mutation routed through ValidateIsReadOnly) on a VirtualizedCellInfoCollection instance obtained where write access is not allowed, e.g. via IList members on the public SelectedCells wrapper.","commonSituations":"Trying to mutate SelectedCells through an IList cast in reflection or generic code; custom selection management code that assumes a writable collection; using the internal collection outside its sanctioned mutation paths.","solutions":["Check IsReadOnly before mutating and fall back to DataGrid selection APIs.","Use the supported mutators (SelectedCells.Add/Remove/Clear) which respect selection rules.","Set selection through DataGrid row/cell IsSelected properties or DataGrid.SelectedItem instead.","Wrap mutation in try-catch (NotSupportedException) for defensive generic code."],"exampleFix":"// before\nvar list = (ICollection<DataGridCellInfo>)dataGrid.SelectedCells;\nlist.Clear();\n// after\ndataGrid.SelectedCells.Clear(); // or clear via grid APIs\nforeach (var cell in dataGrid.SelectedCells.ToList()) { /* read-only iteration */ }","handlingStrategy":"validation","validationCode":"if (collection.IsReadOnly) throw new NotSupportedException(\"SelectedCells is read-only\");","typeGuard":null,"tryCatchPattern":"try { collection.Add(cell); } catch (NotSupportedException) { /* mutate via DataGrid selection APIs instead */ }","preventionTips":["Check IsReadOnly before any mutation","Drive selection through DataGrid/cell IsSelected properties"],"tags":["wpf","datagrid","readonly","notsupported"],"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"}