Skip to content

fix: auto-width columns shrink when widest row is removed#6590

Closed
xbrxr03 wants to merge 1 commit into
Textualize:mainfrom
xbrxr03:fix/datatable-auto-width-shrink
Closed

fix: auto-width columns shrink when widest row is removed#6590
xbrxr03 wants to merge 1 commit into
Textualize:mainfrom
xbrxr03:fix/datatable-auto-width-shrink

Conversation

@xbrxr03

@xbrxr03 xbrxr03 commented Jun 22, 2026

Copy link
Copy Markdown

Summary

Fixes #3449 — DataTable auto-width columns do not shrink back down when the row containing the widest cell is removed.

The Bug

When remove_row() is called on a DataTable with auto-width columns, the column widths are never recalculated. This means removing the row that contained the widest cell in a column leaves the column stuck at its old width, even though all remaining rows are narrower.

This is because remove_row() deletes the row data and updates row locations, but never triggers a column width recalculation. The existing _update_column_widths() method only handles cells that were explicitly updated (via the updated_cells set), so it cannot detect when a removed row was the one setting the maximum width.

The Fix

Added _recalculate_auto_width_columns() which recomputes content_width for each auto-width column by measuring all remaining cells after row removal. Called from remove_row() after deleting the row data but before refreshing the layout.

Key behaviors:

  • If no rows remain, column width collapses to just the label width
  • If rows remain, column width becomes max(widest remaining cell, label width)
  • Fixed-width columns (auto_width=False) are unaffected

Testing

  • ✅ New test: test_remove_row_auto_width_columns_shrink — verifies that removing the widest row shrinks the column width
  • ✅ All 88 existing DataTable tests pass
  • ✅ Reproducible with the example app from the issue (press R to remove row, column now shrinks)

Reproduction

The original issue includes a complete reproduction app. With this fix:

  1. Run the example app — columns are sized to fit the widest content
  2. Press R to remove the row with "Aleksandr Sadovnikov =============" (the widest cell)
  3. Before fix: Column stays wide ✗
  4. After fix: Column shrinks to fit remaining content ✓

When remove_row() is called on a DataTable with auto-width columns,
the column widths were never recalculated. This meant that removing
the row containing the widest cell in a column left the column stuck
at its old width, even though the remaining rows were narrower.

The fix adds _recalculate_auto_width_columns() which recomputes
content_width for each auto-width column by measuring all remaining
cells after row removal. This is called from remove_row() after
deleting the row data but before refreshing the layout.

Fixes Textualize#3449
@willmcgugan

Copy link
Copy Markdown
Member

Your PR has been closed due to a AI policy violation.

Please read the following before submitting further PRs.

https://github.com/Textualize/textual/blob/main/AI_POLICY.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataTable auto-width columns don't shrink back down

3 participants