{"record":{"id":"0b67a01cea12aa1c","repo":"dotnet/wpf","slug":"sr-datagrid-columndisplayindexoutofrange","errorCode":null,"errorMessage":"SR.DataGrid_ColumnDisplayIndexOutOfRange","messagePattern":"SR\\.DataGrid_ColumnDisplayIndexOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs","lineNumber":475,"sourceCode":"        }\n\n        private void InitializeDisplayIndexMap(DataGridColumn changingColumn, int oldDisplayIndex, out int resultDisplayIndex)\n        {\n            resultDisplayIndex = oldDisplayIndex;\n            if (_displayIndexMapInitialized)\n            {\n                return;\n            }\n\n            _displayIndexMapInitialized = true;\n\n            Debug.Assert(DisplayIndexMap.Count == 0, \"DisplayIndexMap should be empty until first measure call.\");\n            int columnCount = Count;\n            Dictionary<int, int> assignedDisplayIndexMap = new Dictionary<int, int>(); // <DisplayIndex, ColumnIndex>\n\n            if (changingColumn != null && oldDisplayIndex >= columnCount)\n            {\n                throw new ArgumentOutOfRangeException(\"displayIndex\", oldDisplayIndex, SR.Format(SR.DataGrid_ColumnDisplayIndexOutOfRange, changingColumn.Header));\n            }\n\n            // First loop:\n            // 1. Validate all columns DisplayIndex\n            // 2. Add columns with DisplayIndex!=default to the assignedDisplayIndexMap\n            for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)\n            {\n                DataGridColumn currentColumn = this[columnIndex];\n                int currentColumnDisplayIndex = currentColumn.DisplayIndex;\n\n                ValidateDisplayIndex(currentColumn, currentColumnDisplayIndex);\n\n                if (currentColumn == changingColumn)\n                {\n                    currentColumnDisplayIndex = oldDisplayIndex;\n                }\n\n                if (currentColumnDisplayIndex >= 0)","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs#L457-L493","documentation":"DataGridColumnCollection.InitializeDisplayIndexMap throws ArgumentOutOfRangeException with DataGrid_ColumnDisplayIndexOutOfRange when a column being processed has a DisplayIndex >= the number of columns. DisplayIndex must be a valid position within the collection; the check runs while building the display-index map.","triggerScenarios":"Setting a column's DisplayIndex to a value greater than or equal to Columns.Count (outside first-measure/init); programmatically assigning DisplayIndex before other columns exist; XAML where DisplayIndex exceeds the column count.","commonSituations":"Assigning DisplayIndex=5 to the only column; restoring saved layouts where columns were removed but saved DisplayIndex values kept; template/default DisplayIndex values conflicting with a reduced column set.","solutions":["Set DisplayIndex values within [0, Columns.Count-1] for the current column count","Rely on automatic DisplayIndex assignment (omit explicit values) where possible","Reassign DisplayIndex after all columns are added","Fix saved/serialized layouts to clamp DisplayIndex to the new column count"],"exampleFix":"// before\nvar col = new DataGridTextColumn { DisplayIndex = 5 };\ngrid.Columns.Add(col); // grid has 2 columns\n// after\nvar col = new DataGridTextColumn();\ngrid.Columns.Add(col);\ncol.DisplayIndex = Math.Min(5, grid.Columns.Count - 1);","handlingStrategy":"validation","validationCode":"if (displayIndex < 0 || displayIndex >= grid.Columns.Count)\n    displayIndex = Math.Max(0, grid.Columns.Count - 1); // clamp","typeGuard":null,"tryCatchPattern":"try { column.DisplayIndex = i; }\ncatch (ArgumentOutOfRangeException) { column.DisplayIndex = grid.Columns.Count - 1; }","preventionTips":["Assign DisplayIndex only after all columns are added","Clamp saved DisplayIndex values when restoring layouts","Let the DataGrid auto-assign DisplayIndex when order does not matter"],"tags":["wpf","datagrid","displayindex","argument-out-of-range"],"backgroundTag":"value-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"}