{"record":{"id":"1c2a91169d554214","repo":"spectreconsole/spectre.console","slug":"cannot-add-new-columns-to-table-with-existing-rows","errorCode":null,"errorMessage":"Cannot add new columns to table with existing rows.","messagePattern":"Cannot add new columns to table with existing rows\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console/Widgets/Table/Table.cs","lineNumber":94,"sourceCode":"    /// </summary>\n    public Table()\n    {\n        _columns = [];\n        Rows = new TableRowCollection(this);\n    }\n\n    /// <summary>\n    /// Adds a column to the table.\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 Table AddColumn(TableColumn column)\n    {\n        ArgumentNullException.ThrowIfNull(column);\n\n        if (Rows.Count > 0)\n        {\n            throw new InvalidOperationException(\"Cannot add new columns to table with existing rows.\");\n        }\n\n        _columns.Add(column);\n        return this;\n    }\n\n    /// <inheritdoc/>\n    protected override Measurement Measure(RenderOptions options, int maxWidth)\n    {\n        ArgumentNullException.ThrowIfNull(options);\n\n        var measurer = new TableMeasurer(this, options);\n\n        // Calculate the total cell width\n        var totalCellWidth = measurer.CalculateTotalCellWidth(maxWidth);\n\n        // Calculate the minimum and maximum table width\n        var measurements = _columns.Select(column => measurer.MeasureColumn(column, totalCellWidth));","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console/Widgets/Table/Table.cs#L76-L112","documentation":"Thrown by Table.AddColumn when at least one row already exists. Like the Grid, the table fixes its column schema once the first row is added; adding columns afterward would break row/column alignment guarantees.","triggerScenarios":"Calling AddColumn on a Table after one or more AddRow calls.","commonSituations":"Data-driven table building where rows are populated in a loop before columns are finalized, or refactoring that moved AddColumn calls after row creation.","solutions":["Register all columns before adding any rows.","Reorder builder code so AddColumn calls precede AddRow calls.","Build a new Table instance if the schema must change after data is present."],"exampleFix":"// before\nvar table = new Table();\ntable.AddRow(\"a\", \"b\");\ntable.AddColumn(new TableColumn(\"Col1\")); // throws\n\n// after\nvar table = new Table();\ntable.AddColumn(new TableColumn(\"Col1\"));\ntable.AddColumn(new TableColumn(\"Col2\"));\ntable.AddRow(\"a\", \"b\");","handlingStrategy":"validation","validationCode":"// Ensure all AddColumn calls happen before any AddRow.\nif (table.Rows.Count > 0) throw new InvalidOperationException(\"Cannot add columns after rows\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build the column schema fully before adding rows.","Refactor to avoid interleaving AddColumn and AddRow calls."],"tags":["table","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"}