Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do ColumnStyles and RowStyles work with TableLayoutPanel?

The ColumnStyles and RowStyles collection control the sizing of all the rows and columns.  If these are empty or there are more rows/columns than styles, it is assumed the column/row is "AutoSized". 

  • AutoSize – sizes the column/row to the minimum size necessary to fully contain the largest content in the column/row.
  • Absolute – sizes to a fixed width
  • Percent – sizes to a normalized percentage of remaining space.

NOTE: Although not strictly required, at design time we attempt to preserve a 1:1 mapping between row and rowstyle along with column and columnstyle. Because these collections in runtime are independent, alteration of one collection can result in sync problems between the two and unexpected results.

Row/Column space distribution

The Row and Column Styles collection are used to determine how to allocate space within the table. Rows and columns are allocated space in a non-autosized TableLayoutPanel in the following order:

  1. Absolutely columns/rows (full space allocated)
  2. AutoSized columns/rows (as much as possible to fit the controls in the column/row)
  3. Percentage columns/rows (distribution of remaining space)

Example I: Percentage styled columns attempt to fill the remaining space.  So a 200px wide table with 20px absolute, an auto sized column and a 100% column would go like this:

  1. col1 ABS: 20 px
  2. col2 AUTO: whatever the preferred size is... say this measures to be 100px
  3. col3 100%: 200 - 20 - 100 = 80px.

Example II: If there are two percentage style columns, the 80 pixels would be divied up between the two columns in proportion to one another - so if we had 20% and 80% it would be:

  1. col1 ABS: 20 px
  2. col2 AUTO: whatever the preferred size is... say this measures to be 100px
  3. col3 20%: (200 - 20 – 100) = 80px remaining space.          80px * .20  = 16px.
  4. col4 80%: (200 - 20 – 100) = 80px remaining space.          80px * .80  = 64px.

Example III: Exactly the same as above but the two percentage style columns are both 75%, the 80 pixels would be split equally between the two normalized columns:

  1. col1 ABS: 20 px
  2. col2 AUTO: whatever the preferred size is... say this measures to be 100px
  3. col3 75% (normalizes to 50%): (200 - 20 – 100) = 80px remaining space.     80px * .50  = 40px.
  4. col4 75% (normalizes to 50%): (200 - 20 – 100) = 80px remaining space.     80px * .50  = 40px.

Example IV: The last column covers the table layout panels underwear. So a 200px wide table with 20px absolute, an auto sized column and a 20px absolute would go like this:

  1. col1 ABS: 20 px
  2. col2 AUTO: whatever the preferred size is... say this measures to be 100px
  3. col3 ABS: 20 px, not enough pixels so we stretch the last column to be (200 - 20 – 100) = 80px


Page view counter