dotnet/wpf · error · ArgumentOutOfRangeException

SR.GridRowOutOfRange

Error message

SR.GridRowOutOfRange

What it means

WindowsListView's IGridProvider.GetItem throws ArgumentOutOfRangeException with SR.GridRowOutOfRange when the row parameter is negative or >= the list-view's current row count (GetRowCount). The requested grid cell is outside the control's grid extent.

Solutions

  1. Read GridPattern.RowCount (or the list count) immediately before calling GetItem and bound the loop with it.
  2. Clamp or validate the row index before the call.
  3. Catch ArgumentOutOfRangeException and re-query the current count.
  4. Refresh the automation element after list changes before indexing.

Example fix

// before
for (int r = 0; r <= cachedCount; r++) grid.GetItem(r, 0);
// after
int count = grid.Current.RowCount;
for (int r = 0; r < count; r++) grid.GetItem(r, 0);
Defensive patterns

Strategy: validation

Validate before calling

int rows = ((GridPattern)element.GetCurrentPattern(GridPattern.Pattern)).Current.RowCount;
if (row >= 0 && row < rows) { grid.GetItem(row, column); }

Try / catch

try { grid.GetItem(row, col); } catch (ArgumentOutOfRangeException) { /* row no longer exists; re-query RowCount */ }

Prevention

When it happens

Trigger: Calling GetItem(row, column) with row < 0 or row >= GetRowCount(_hwnd) — e.g. row 5 on a list that now has only 3 items after a refresh.

Common situations: Test scripts caching item counts from before a list refresh; iterating rows with an off-by-one bound; automating virtual-mode lists whose counts changed.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListView.cs:741

            {
                return (bool) WindowScroll.GetPropertyScroll (ScrollPattern.VerticallyScrollableProperty, _hwnd);
            }
        }

        #endregion ScrollPattern

        #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)
        {
            int maxRow = GetRowCount (_hwnd);
            int maxColumn = GetColumnCount (_hwnd);

            if (row < 0 || row >= maxRow)
            {
                throw new ArgumentOutOfRangeException(nameof(row), row, SR.GridRowOutOfRange);
            }

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

            // GetCell
            if (IsDetailMode (_hwnd))
            {
                return GetCellInDetailMode (row, column);
            }

            return GetCellInOtherModes (row, column, maxColumn, maxRow);
        }

        int IGridProvider.RowCount
        {

View on GitHub (pinned to 81131a70a4)