How to let table layout fit 100% of the container with

botenvouwer picture botenvouwer · Nov 21, 2013 · Viewed 7.7k times · Source

How can I give a tablelayoutpanel a width of 100% so it will fill the parent container and also resizes the table when resizing the window.

My form looks like this now:

enter image description here

I want to add rows dynamically so the result will be some like this:

enter image description here

It would be nice if the table would fit the splitcontainer panel. Someone knows how to do this?

This is my current code to add rows to the table:

tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Outset;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;

tableLayoutPanel1.Controls.Add(new Label() { Text = "first row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "second row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "third row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "4th row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "5th row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "6th row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "7th row:", Anchor = AnchorStyles.Left, AutoSize = true });

I can set the column and rows to a width or height of 100% so how can I just set the table with to 100%?

// 
// tableLayoutPanel1
// 
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(754, 169);
//this.tableLayoutPanel1.Size = new System.Drawing.Size(100F, 169);//pseudo code
this.tableLayoutPanel1.TabIndex = 0;

Answer

Sebastian Piu picture Sebastian Piu · Nov 21, 2013

You need to set the DockStyle to Fill

You can do this in the designer using the Dock property and selecting Fill from the dropdown, or by code:

this.tableLayoutPanel1.Dock = DockStyle.Fill