spectreconsole/spectre.console · error · InvalidOperationException

The number of row columns are greater than the number of gri

Error message

The number of row columns are greater than the number of grid columns.

What it means

Thrown by Grid.AddRow when the number of IRenderable columns passed exceeds the number of columns registered via AddColumn. Each row must not have more cells than the grid has columns.

Source

Thrown at src/Spectre.Console/Widgets/Grid.cs:89

        _padRightCell = column.HasExplicitPadding;

        _columns.Add(column);

        return this;
    }

    /// <summary>
    /// Adds a new row to the grid.
    /// </summary>
    /// <param name="columns">The columns to add.</param>
    /// <returns>The same instance so that multiple calls can be chained.</returns>
    public Grid AddRow(params IRenderable[] columns)
    {
        ArgumentNullException.ThrowIfNull(columns);

        if (columns.Length > _columns.Count)
        {
            throw new InvalidOperationException("The number of row columns are greater than the number of grid columns.");
        }

        _rows.Add(new GridRow(columns));
        return this;
    }

    /// <inheritdoc/>
    protected override bool HasDirtyChildren()
    {
        return _columns.Any(c => ((IHasDirtyState)c).IsDirty);
    }

    /// <inheritdoc/>
    protected override IRenderable Build()
    {
        var table = new Table
        {
            Expand = Expand,

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Count the registered columns and ensure each row has that many or fewer items.
  2. Add the missing columns before adding the row.
  3. Trim the row items to match the column count.

Example fix

// before
var grid = new Grid().AddColumn();
grid.AddRow(new Text("a"), new Text("b")); // throws: 2 > 1

// after
var grid = new Grid().AddColumn().AddColumn();
grid.AddRow(new Text("a"), new Text("b"));
Defensive patterns

Strategy: validation

Validate before calling

if (rowItems.Length > grid.Columns.Count()) throw new InvalidOperationException("Row has too many items");
grid.AddRow(rowItems);

Prevention

When it happens

Trigger: Calling AddRow with more arguments than the count of AddColumn calls previously made.

Common situations: Mistakenly passing extra items, a data-driven loop adding more values than configured columns, or forgetting to add a required column.

Related errors


AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13). Data as JSON: /api/errors/d8ee598e4502bddd. Report an issue: GitHub.