{"record":{"id":"96d388089141a860","repo":"dotnet/wpf","slug":"exception-of-type-system-invalidoperationexception-was","errorCode":null,"errorMessage":"Exception of type 'System.InvalidOperationException' was thrown.","messagePattern":"Exception of type 'System\\.InvalidOperationException' was thrown\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MultipleCopiesCollection.cs","lineNumber":290,"sourceCode":"        void IList.RemoveAt(int index)\n        {\n            throw new NotSupportedException(SR.DataGrid_ReadonlyCellsItemsSource);\n        }\n\n        public object this[int index]\n        {\n            get\n            {\n                ArgumentOutOfRangeException.ThrowIfNegative(index);\n                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, RepeatCount);\n\n                Debug.Assert(_item != null, \"_item should be non-null.\");\n                return _item;\n            }\n\n            set\n            {\n                throw new InvalidOperationException();\n            }\n        }\n\n        #endregion\n\n        #region ICollection Members\n\n        public void CopyTo(Array array, int index)\n        {\n            throw new NotSupportedException();\n        }\n\n        public int Count\n        {\n            get { return RepeatCount; }\n        }\n\n        public bool IsSynchronized","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MultipleCopiesCollection.cs#L272-L308","documentation":"MultipleCopiesCollection represents one repeated item, so the indexer's setter is meaningless and is implemented as `throw new InvalidOperationException()` in the indexor at MultipleCopiesCollection.cs:290. Replacing the value at any index is not a supported operation on this internal collection.","triggerScenarios":"Assigning through the indexer, e.g. `collection[i] = newItem`, typically via an IList-typed reference on the DataGrid cells presenter's ItemsSource.","commonSituations":"Generic editing code that swaps elements in any IList; attempts to patch a cell's content by writing into the presenter ItemsSource rather than the underlying data item.","solutions":["Do not write into the presenter's ItemsSource; edit the underlying data object or the collection bound to DataGrid.ItemsSource instead.","Replace the whole item in your bound ObservableCollection if the row content must change.","Cast checks: guard generic IList-editing helpers to skip read-only/fixed-size collections (IList.IsReadOnly / IsFixedSize)."],"exampleFix":"// before\ncellsPresenter.ItemsSource[i] = newItem;\n\n// after\nboundRows[i] = newItem; // mutate the ObservableCollection bound to DataGrid.ItemsSource","handlingStrategy":"validation","validationCode":"if (itemsSource is IList { IsReadOnly: true } or IList { IsFixedSize: true })\n    throw new InvalidOperationException(\"Indexer writes are not allowed on this read-only collection.\");","typeGuard":"bool SupportsIndexerWrite(object c) => c is IList l && !l.IsReadOnly && !l.IsFixedSize;","tryCatchPattern":"try { list[i] = value; }\ncatch (InvalidOperationException) { /* edit the underlying data item instead */ }","preventionTips":["Edit data items or the bound collection, never the generated presenter collection","Guard generic IList-edit helpers with IsReadOnly/IsFixedSize checks"],"tags":["wpf","datagrid","read-only-collection","invalidoperationexception"],"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"}