Title: TableLayoutPanel: set number of columns dynamically when windows resized
Tags: winforms;c#-4.0
Question: I have a TableLayoutPanel on a windows form, how to set the number of columns on the fly when the form window re-sizes? For example, 3 columns if the form is maximized, and 2 columns when the form is re-sized to a smaller size?
Thanks for any help on this!
Comment: What if the user needs to look at data in the third column while the form is in the smaller size?

Comment: Never try to turn a TLP in a grid control.  It will suck heavily, not what it was made to do.

Comment: There is only labels in the tablelayoutpanel, I have a datagrid on the form too, user will click on rows in the datagrid to select and the selected values will then become labels in the tablelayoutpanel. This way, user can see what values he has selected from the data grid. Since it is a tablelayoutpanel, all values will be lay out in the panel automatically. No matter if its 3 columns or 2 columns. What I want is to set the number of columns automatically based on the window size.

Here is the accepted answer: Thanks for your help, Sayka.
Here is the final resolution I used. I post it here just in case someone may need it later.
The TableLayoutPanel was set to grow "AddRows" since the it needs to grow both rows and columns and I had the following code to handle columns.
I had two for loops to remove the existing Column styles since one for loop will not remove all, it will leave the last one in there. Maybe its not the best solution, but it worked.
The TableLayoutPanel is inside a groupbox, and here is the the sizeChanged event of groupbox:
private void groupBox2_SizeChanged(object sender, EventArgs e)
int avaiableWidth = this.groupBox2.Width;
int maxLableWidth = 150; //button width = 140 + 10 margin
this.tlpButtons.ColumnCount = avaiableWidth / maxLableWidth;
List<ColumnStyle&gt; stylesToRemove = new List<ColumnStyle&gt;();
foreach (ColumnStyle style in tlpButtons.ColumnStyles)
stylesToRemove.Add(style);
foreach (ColumnStyle style in stylesToRemove)
tlpButtons.ColumnStyles.Remove(style);
for (int i = 0; i < this.tlpButtons.ColumnCount; i++)
{
ColumnStyle c = new ColumnStyle();
c.SizeType = SizeType.Percent;
c.Width = Convert.ToSingle(Math.Floor((decimal)100 / (decimal)this.tlpButtons.ColumnCount));
this.tlpButtons.ColumnStyles.Add(c);
}
this.tlpButtons.Refresh();
Welcome any suggestions or better ideas!

Here is another answer: Larger:
panel.ColumnCount = 3;
Smaller:
panel.ColumnCount = 2;

Here is another answer: Here is a sample where you can add columns dynamically with a desired width you wish for the columns.
In the TableLayoutPanel :
Set the GrowStyle to AddColumns
To Check the result
Set Dockstyle to fill and
Set CellBorderStyle to Single
Now add a resize event to the form..
public Form1()
InitializeComponent();
this.Resize += Form1_Resize;
void Form1_Resize(object sender, EventArgs e)
int desiredWidthPerColumn = 100;
setColumnsWidth(desiredWidthPerColumn, tableLayoutPanel1);
void setColumnsWidth(int width, TableLayoutPanel tPanel)
tPanel.ColumnCount = tPanel.Width / width;
for (int i = 0; i < tPanel.ColumnStyles.Count; i++) tPanel.ColumnStyles.RemoveAt(i);
for (int i = 0; i < tPanel.ColumnCount; i++) tPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, width));

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:09/12/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<winforms><c#-4.0>', 'Title': 'TableLayoutPanel: set number of columns dynamically when windows resized', 'ViewCount': 503, 'id': 25814353, 'user_AboutMe': '', 'user_AccountId': 4929125.0, 'user_DisplayName': 'Moli Little', 'user_Reputation': 1.0, 'user_Views': 1.0, 'user_id': 3969500.0}}