{"record":{"id":"413cf51ee640b81d","repo":"dotnet/wpf","slug":"the-itemssource-for-the-datagridcellspresenter-is-read-only","errorCode":null,"errorMessage":"The ItemsSource for the DataGridCellsPresenter is read-only.","messagePattern":"The ItemsSource for the DataGridCellsPresenter is read-only\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MultipleCopiesCollection.cs","lineNumber":230,"sourceCode":"\n        private void OnReplace(object oldItem, object newItem, int index)\n        {\n            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItem, oldItem, index));\n        }\n\n        private void Reset()\n        {\n            RepeatCount = 0;\n            OnCollectionReset();\n        }\n\n        #endregion\n\n        #region IList Members\n\n        public int Add(object value)\n        {\n            throw new NotSupportedException(SR.DataGrid_ReadonlyCellsItemsSource);\n        }\n\n        public void Clear()\n        {\n            throw new NotSupportedException(SR.DataGrid_ReadonlyCellsItemsSource);\n        }\n\n        public bool Contains(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            return _item == value;\n        }\n\n        public int IndexOf(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MultipleCopiesCollection.cs#L212-L248","documentation":"MultipleCopiesCollection is the read-only collection the DataGridCellsPresenter exposes as its ItemsSource, containing one copy of the row's cell item per column. IList.Add is deliberately non-functional and throws NotSupportedException (SR.DataGrid_ReadonlyCellsItemsSource) because the collection is generated and owned by the DataGrid from its columns; callers must never mutate it.","triggerScenarios":"Calling Add on DataGridCellsPresenter.Items or its ItemsSource, directly or through an IList reference (e.g. ((IList)presenter.Items).Add(obj)); also if ItemsControl machinery attempts to add into it.","commonSituations":"Trying to append an extra cell to a DataGrid row programmatically; automation/test code that manipulates cell collections; reusing code written against normal editable ItemsControl collections.","solutions":["Do not modify the cells collection; add or remove DataGridColumns on the owning DataGrid.Columns — cells are regenerated from columns.","Customize cell appearance via Column.CellTemplate/CellStyle or DataGrid events (AutoGeneratingColumn, LoadingRow) instead of injecting cells.","Project to a new List via LINQ (ToList) when you need a modifiable copy.","Catch NotSupportedException at the boundary if consuming third-party code, treating it as a signal the collection is DataGrid-managed."],"exampleFix":"// before: ((IList)cellsPresenter.Items).Add(new DataGridCell()); // throws NotSupportedException // after: dataGrid.Columns.Add(new DataGridTextColumn { Header = \"New\", Binding = new Binding(\"Prop\") });","handlingStrategy":"validation","validationCode":"static bool CanAddItems(ItemsControl presenter) => !(presenter is DataGridCellsPresenter) && !((IList)presenter.Items).IsReadOnly;","typeGuard":"static bool IsMutableList(object list) => list is IList l && !l.IsReadOnly;","tryCatchPattern":"try { cells.Add(newCell); } catch (NotSupportedException ex) when (ex.Message.Contains(\"read-only\")) { dataGrid.Columns.Add(new DataGridTextColumn { Header = \"New\" }); }","preventionTips":["Treat DataGridCellsPresenter.Items as strictly read-only.","Add/remove/reorder DataGridColumns on DataGrid.Columns instead of touching cell collections.","Check IList.IsReadOnly before mutating any Items collection.","Copy to a List<T> with LINQ when a modifiable collection is required."],"tags":["wpf","datagrid","read-only-collection","not-supported","itemscontrol"],"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"}