dotnet/wpf · error · ArgumentOutOfRangeException

SR.GridColumnOutOfRange

Error message

SR.GridColumnOutOfRange

What it means

WindowsListView's IGridProvider.GetItem throws ArgumentOutOfRangeException with SR.GridColumnOutOfRange when the column parameter is negative or >= GetColumnCount(_hwnd). The column index does not exist in the list-view's grid (only meaningful in details view).

Solutions

  1. Query GridPattern.ColumnCount right before GetItem and validate the column index.
  2. Handle view-mode changes: re-fetch column count after switching views.
  3. Catch ArgumentOutOfRangeException and skip missing columns.
  4. Cache column indexes by header name and resolve them dynamically.

Example fix

// before
var cell = grid.GetItem(row, 3);
// after
if (3 < grid.Current.ColumnCount) { var cell = grid.GetItem(row, 3); }
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try { grid.GetItem(row, col); } catch (ArgumentOutOfRangeException) { /* column not present in current view */ }

Prevention

When it happens

Trigger: Calling GetItem(row, column) with column < 0 or column >= GetColumnCount — e.g. column 3 when the list-view shows only 2 columns, or in non-details views where the column count is limited.

Common situations: Column sets changed (columns added/removed) after the script cached them; switching to a view mode with fewer columns; iterating columns with wrong bounds.

Related errors


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

Appendix: source

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

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

View on GitHub (pinned to 81131a70a4)