dotnet/wpf · error · ArgumentOutOfRangeException

SR.GridRowOutOfRange

Error message

SR.GridRowOutOfRange

What it means

WindowsListViewGroup's IGridProvider.GetItem validates the requested row against GetRowCount and throws ArgumentOutOfRangeException with SR.GridRowOutOfRange when row < 0 or row >= maxRow. Grid cell coordinates must fall within the group's current row/column extents.

Solutions

  1. Re-read GridPattern.Current.RowCount immediately before iterating rows.
  2. Validate 0 <= row < RowCount before GetItem.
  3. Use a loop bounded by the freshly queried RowCount rather than hardcoded sizes.

Example fix

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

Strategy: validation

Validate before calling

int maxRow = grid.Current.RowCount;
if (row < 0 || row >= maxRow) return null;

Type guard

bool IsValidRow(int row, int maxRow) => row >= 0 && row < maxRow;

Try / catch

try { cell = grid.GetItem(row, col); }
catch (ArgumentOutOfRangeException) { cell = null; }

Prevention

When it happens

Trigger: Calling GridPattern.GetItem(row, column) on a ListView group item with row < 0 or row >= GetRowCount(_hwnd, ID), e.g. using list-count based assumptions after rows were removed.

Common situations: Automation clients that cached GridPattern.RowCount earlier and iterate with the old count after the ListView data changed; zero-based vs one-based indexing mistakes.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewGroup.cs:366

            }

            return false;
        }

        #endregion Interface Methods

        #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, ID);
            int maxColumn = GetColumnCount(_hwnd, ID);

            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);
            }

            if (WindowsListView.IsDetailMode (_hwnd))
            {
                return GetCellInDetailMode (row, column);
            }

            return GetCellInOtherModes (row, column, maxColumn);
        }

        int IGridProvider.RowCount
        {
            get

View on GitHub (pinned to 81131a70a4)