{"record":{"id":"968b8081cd537534","repo":"spectreconsole/spectre.console","slug":"cannot-add-new-columns-to-grid-with-existing-rows","errorCode":null,"errorMessage":"Cannot add new columns to grid with existing rows.","messagePattern":"Cannot add new columns to grid with existing rows\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console/Widgets/Grid.cs","lineNumber":67,"sourceCode":"    /// <returns>The same instance so that multiple calls can be chained.</returns>\n    public Grid AddColumn()\n    {\n        AddColumn(new GridColumn());\n        return this;\n    }\n\n    /// <summary>\n    /// Adds a column to the grid.\n    /// </summary>\n    /// <param name=\"column\">The column to add.</param>\n    /// <returns>The same instance so that multiple calls can be chained.</returns>\n    public Grid AddColumn(GridColumn column)\n    {\n        ArgumentNullException.ThrowIfNull(column);\n\n        if (_rows.Count > 0)\n        {\n            throw new InvalidOperationException(\"Cannot add new columns to grid with existing rows.\");\n        }\n\n        // Only pad the most right cell if we've explicitly set a padding.\n        _padRightCell = column.HasExplicitPadding;\n\n        _columns.Add(column);\n\n        return this;\n    }\n\n    /// <summary>\n    /// Adds a new row to the grid.\n    /// </summary>\n    /// <param name=\"columns\">The columns to add.</param>\n    /// <returns>The same instance so that multiple calls can be chained.</returns>\n    public Grid AddRow(params IRenderable[] columns)\n    {\n        ArgumentNullException.ThrowIfNull(columns);","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console/Widgets/Grid.cs#L49-L85","documentation":"Thrown by Grid.AddColumn when at least one row already exists. The grid fixes its column count once the first row is added, so adding more columns afterward is structurally forbidden to keep rows consistent.","triggerScenarios":"Calling AddColumn after one or more AddRow calls on the same Grid instance.","commonSituations":"Building a grid procedurally where rows are added in a loop before all columns are registered, or refactoring that reorders AddColumn calls after row creation.","solutions":["Add all columns before adding any rows.","Restructure the builder code so column setup precedes row population.","If dynamic columns are needed later, construct a new Grid instance instead of mutating an existing one."],"exampleFix":"// before\nvar grid = new Grid();\ngrid.AddRow(new Text(\"a\"), new Text(\"b\"));\ngrid.AddColumn(); // throws\n\n// after\nvar grid = new Grid()\n    .AddColumn().AddColumn()\n    .AddRow(new Text(\"a\"), new Text(\"b\"));","handlingStrategy":"validation","validationCode":"// Ensure all AddColumn calls happen before any AddRow.\nif (grid.Rows.Any()) throw new InvalidOperationException(\"Cannot add columns after rows\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Structure builder code as: columns first, then rows.","Track whether rows have been added before calling AddColumn."],"tags":["grid","invalid-operation","ordering","spectre-console"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}