{"record":{"id":"ec830c7303488794","repo":"dotnet/wpf","slug":"sr-datagrid-columnindexoutofrange","errorCode":null,"errorMessage":"SR.DataGrid_ColumnIndexOutOfRange","messagePattern":"SR\\.DataGrid_ColumnIndexOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs","lineNumber":66,"sourceCode":"            if (DisplayIndexMapInitialized)\n            {\n                ValidateDisplayIndex(item, item.DisplayIndex, true);\n            }\n\n            base.InsertItem(index, item);\n            item.CoerceValue(DataGridColumn.IsFrozenProperty);\n        }\n\n        protected override void SetItem(int index, DataGridColumn item)\n        {\n            if (item == null)\n            {\n                throw new ArgumentNullException(nameof(item), SR.DataGrid_NullColumn);\n            }\n\n            if (index >= Count || index < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), SR.Format(SR.DataGrid_ColumnIndexOutOfRange, item.Header));\n            }\n\n            if (item.DataGridOwner != null && this[index] != item)\n            {\n                throw new ArgumentException(SR.Format(SR.DataGrid_InvalidColumnReuse, item.Header), nameof(item));\n            }\n\n            if (DisplayIndexMapInitialized)\n            {\n                ValidateDisplayIndex(item, item.DisplayIndex);\n            }\n\n            base.SetItem(index, item);\n            item.CoerceValue(DataGridColumn.IsFrozenProperty);\n        }\n\n        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)\n        {","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs#L48-L84","documentation":"DataGridColumnCollection.SetItem throws ArgumentOutOfRangeException with DataGrid_ColumnIndexOutOfRange when replacing a column using an index that is >= Count or negative. Only indices of existing columns can be replaced.","triggerScenarios":"dataGrid.Columns[index] = column with index outside [0, Count-1]; off-by-one calculations using Count instead of Count-1; index taken from an external mapping after columns were removed.","commonSituations":"Stale column indices cached before columns were added/removed; loops using <= Count; user-supplied index not validated.","solutions":["Validate 0 <= index && index < Columns.Count before assignment","Recompute indices after any add/remove of columns","Use Contains/the column reference instead of raw indices where possible"],"exampleFix":"// before\ngrid.Columns[i] = newColumn;\n// after\nif (i >= 0 && i < grid.Columns.Count) grid.Columns[i] = newColumn;","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= grid.Columns.Count)\n    throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":null,"tryCatchPattern":"try { grid.Columns[i] = col; }\ncatch (ArgumentOutOfRangeException) { grid.Columns.Add(col); }","preventionTips":["Recompute indices after add/remove operations","Avoid caching column indices across mutations","Prefer column-reference lookups (IndexOf) over stored ints"],"tags":["wpf","datagrid","index-out-of-range","columns"],"backgroundTag":"index-out-of-range","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"}