dotnet/wpf · error · ArgumentOutOfRangeException

SR.GridColumnOutOfRange

Error message

SR.GridColumnOutOfRange

What it means

WindowsCalendar's IGridProvider.GetItem throws ArgumentOutOfRangeException when the column index is outside 0..MAX_DAYS-1 (the calendar grid has one column per day of the week). The row check similarly bounds it to MAX_WEEKS. The automation client addressed a cell outside the calendar grid.

Solutions

  1. Read GridPattern.ColumnCount (7) and keep column within 0..ColumnCount-1.
  2. Validate/clamp the column index before calling GetItem.
  3. Switch to 0-based indexing if coming from a 1-based mental model.
  4. Catch ArgumentOutOfRangeException and correct the index in the caller.

Example fix

// before
var item = gridPattern.GetItem(row, column); // column from 1..7
// after
var item = gridPattern.GetItem(row, column - 1); // convert to 0-based
Defensive patterns

Strategy: validation

Validate before calling

if (column >= 0 && column < gridPattern.ColumnCount) { var item = gridPattern.GetItem(row, column); }

Try / catch

try { gridPattern.GetItem(row, column); } catch (ArgumentOutOfRangeException) { /* clamp column to ColumnCount-1 */ }

Prevention

When it happens

Trigger: Calling IGridProvider.GetItem(row, column) with column < 0 or column >= MAX_DAYS (e.g. column 7 for a 7-day week, zero-based).

Common situations: UIA clients assuming 1-based columns, or mapping screen coordinates to grid cells with off-by-one errors.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/2388e86f40b78c85. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs:2520

                return lastChild;
            }

            #endregion

            #region Grid Pattern

            // Obtain the AutomationElement at an zero based absolute position in the grid.  
            // Where 0,0 is top left
            IRawElementProviderSimple IGridProvider.GetItem(int row, int column)
            {
                if (row < 0 || row >= MAX_WEEKS)
                {
                    throw new ArgumentOutOfRangeException("row", row, SR.GridRowOutOfRange);
                }

                if (column < 0 || column >= MAX_DAYS)
                {
                    throw new ArgumentOutOfRangeException("column", column, SR.GridColumnOutOfRange);
                }

                int dayIndex = CalendarDay.DayIndexFromRowColumn(row, column);
                return CreateCalendarDay(dayIndex);
            }

            // Number of Rows for the grid
            int IGridProvider.RowCount
            {
                get
                {
                    return MAX_WEEKS;
                }
            }

            // Number of Columns for the grid
            int IGridProvider.ColumnCount
            {

View on GitHub (pinned to 81131a70a4)