{"record":{"id":"b7526ad25c487b54","repo":"dotnet/wpf","slug":"sr-datagrid-displayindexoutofrange","errorCode":null,"errorMessage":"SR.DataGrid_DisplayIndexOutOfRange","messagePattern":"SR\\.DataGrid_DisplayIndexOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs","lineNumber":383,"sourceCode":"            }\n\n            // we're not going to retry\n            BringColumnIntoViewRetryCountField.ClearValue(this);\n            return false;\n        }\n\n        #endregion\n\n        #region Display Index\n\n        /// <summary>\n        ///     Returns the DataGridColumn with the given DisplayIndex\n        /// </summary>\n        public DataGridColumn ColumnFromDisplayIndex(int displayIndex)\n        {\n            if (displayIndex < 0 || displayIndex >= Columns.Count)\n            {\n                throw new ArgumentOutOfRangeException(nameof(displayIndex), displayIndex, SR.DataGrid_DisplayIndexOutOfRange);\n            }\n\n            return InternalColumns.ColumnFromDisplayIndex(displayIndex);\n        }\n\n        /// <summary>\n        ///     Event that is fired when the DisplayIndex on one of the DataGrid's Columns changes.\n        /// </summary>\n        public event EventHandler<DataGridColumnEventArgs> ColumnDisplayIndexChanged;\n\n        /// <summary>\n        ///     Called when the DisplayIndex of a column is modified.\n        /// </summary>\n        /// <remarks>\n        ///     A column's DisplayIndex may be modified as the result of another column's DisplayIndex changing.  This is because the\n        ///     DataGrid enforces that the DisplayIndex of all Columns are unique integers from 0 to Columns.Count -1.\n        /// </remarks>\n        protected internal virtual void OnColumnDisplayIndexChanged(DataGridColumnEventArgs e)","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs#L365-L401","documentation":"DataGrid.ColumnFromDisplayIndex maps a column's DisplayIndex to the corresponding DataGridColumn and throws ArgumentOutOfRangeException(SR.DataGrid_DisplayIndexOutOfRange) when displayIndex is negative or >= Columns.Count. DisplayIndex is a zero-based position within the displayed column order, not the column collection index.","triggerScenarios":"Calling ColumnFromDisplayIndex with a value outside [0, Columns.Count-1] — e.g. saved DisplayIndex from a previous session, a value from user input, or an index computed before columns were added/reordered.","commonSituations":"Persisting/restoring column layouts where the user removed columns, computing display indexes from another grid's layout, or using the index during column add/remove events when the collection state differs.","solutions":["Validate 0 <= displayIndex && displayIndex < dataGrid.Columns.Count before calling.","Clamp or recompute stored DisplayIndex values after column count changes when restoring layouts.","Compare against dataGrid.Columns.Count at call time, not a cached count.","Catch ArgumentOutOfRangeException around layout-restore code and skip invalid entries."],"exampleFix":"// before\nvar col = dataGrid.ColumnFromDisplayIndex(savedIndex); // may throw\n// after\nif (savedIndex >= 0 && savedIndex < dataGrid.Columns.Count)\n    var col = dataGrid.ColumnFromDisplayIndex(savedIndex);","handlingStrategy":"validation","validationCode":"bool inRange = displayIndex >= 0 && displayIndex < dataGrid.Columns.Count;","typeGuard":"static bool IsValidDisplayIndex(DataGrid grid, int displayIndex) => displayIndex >= 0 && displayIndex < grid.Columns.Count;","tryCatchPattern":"try\n{\n    var col = dataGrid.ColumnFromDisplayIndex(displayIndex);\n}\ncatch (ArgumentOutOfRangeException)\n{\n    // stale/persisted index; recompute from current layout\n}","preventionTips":["Validate displayIndex against Columns.Count at call time","Clamp persisted layout indexes when column count changes","Recompute indexes after column add/remove/reorder events"],"tags":["wpf","datagrid","argument-out-of-range","columns"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}