dotnet/wpf · error · ArgumentOutOfRangeException

SR.GridRowOutOfRange

Error message

SR.GridRowOutOfRange

What it means

IGridProvider.GetItem on WindowsCalendar validates the row index against MAX_WEEKS (6). A row outside 0..MAX_WEEKS-1 raises ArgumentOutOfRangeException with message SR.GridRowOutOfRange, because the MonthCalendar grid only has a fixed number of week rows.

Solutions

  1. Read GridPattern.RowCount from the element and keep row within 0..RowCount-1.
  2. Clamp or validate the row index before calling GetItem.
  3. Catch ArgumentOutOfRangeException and fall back to the valid range discovered via RowCount.
  4. Use the element's supported children discovery (FindChildren) instead of guessing row indices.

Example fix

// before
var item = gridPattern.GetItem(row, column);
// after
int rowCount = gridPattern.RowCount;
if (row >= 0 && row < rowCount)
{
    var item = gridPattern.GetItem(row, column);
}
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Calling IGridProvider.GetItem(row, column) on the calendar element with row < 0 or row >= MAX_WEEKS (e.g. row 6 or 7 when at most 6 weeks exist).

Common situations: UIA clients computing grid coordinates from screen positions or iterating rows without consulting the grid's RowCount property; assumptions based on other grid controls.

Related errors


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

Appendix: source

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

                }
                else
                {
                    lastChild = new CalendarDay(_hwnd, this, TOTAL_CELLS - 1, _iCalendar);
                }
                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;
                }

View on GitHub (pinned to 81131a70a4)