dotnet/wpf · error · ArgumentOutOfRangeException

SR.GridColumnOutOfRange

Error message

SR.GridColumnOutOfRange

What it means

Same IGridProvider.GetItem path as the row check, but for the column: WindowsListViewGroup.GetItem throws ArgumentOutOfRangeException with SR.GridColumnOutOfRange when column < 0 or column >= GetColumnCount(_hwnd, ID). In detail mode the column maps to a ListView subitem, so only existing columns are addressable.

Solutions

  1. Query GridPattern.Current.ColumnCount (or GetColumnCount) fresh before each GetItem loop.
  2. Validate 0 <= column < ColumnCount before calling GetItem.
  3. Guard view-mode dependent columns: only access extra columns when IsDetailMode is true.

Example fix

// before
for (int c = 0; c <= headers; c++) grid.GetItem(r, c);
// after
int maxCol = grid.Current.ColumnCount;
for (int c = 0; c < maxCol; c++) grid.GetItem(r, c);
Defensive patterns

Strategy: validation

Validate before calling

int maxCol = grid.Current.ColumnCount;
if (column < 0 || column >= maxCol) return null;

Type guard

bool IsValidColumn(int col, int maxCol) => col >= 0 && col < maxCol;

Try / catch

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

Prevention

When it happens

Trigger: Calling GridPattern.GetItem(row, column) with column < 0 or column >= GetColumnCount, e.g. iterating 0..ColumnHeader.Count of the full ListView when this group exposes fewer columns, or addressing a column that only exists in detail mode.

Common situations: Clients assuming the grid always has a fixed number of columns while the control is in a view mode with fewer subitems; off-by-one loops using <= ColumnCount.

Related errors


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

Appendix: source

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

        #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
            {
                return GetRowCount (_hwnd, ID);
            }
        }

View on GitHub (pinned to 81131a70a4)