I've been fighting with this for a while, and have found that a number of other people struggle with the TableLayoutPanel (.net 2.0 Winforms) as well.
Problem
I am attempting to take a 'blank' tablelayoutpanel, which has 10 columns defined, then at runtime programmatically add rows of controls (i.e. one control per cell).
One might have thought that it should be as simple as
myTableLayoutPanel.Controls.Add(myControl, 0 /* Column Index */, 0 /* Row index */);
But that (for me) doesn't add the rows. So maybe adding in a row style
myTableLayoutPanel.RowStyles.Clear();
myTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
But that doesn't work either. I've dug around and found out that the myTableLayoutPanel.RowCount
usage changes from design time to run time, hence doing myTableLayoutPanel.RowCount++;
doesn't actually add another row, not even before/after adding a RowStyle entry for it!
Another related issue I am encountering is that the controls will be added to the display, but they all simply get rendered at point 0,0 of the TableLayoutPanel, additionally they are not even constrained to be within the Cell bounds that they are supposed to be displayed within (i.e. with Dock = DockStyle.Fill they still appear way too large/small).
Does someone have a working example of adding rows & controls at runtime?
I just had a related problem (which is how I found this thread), where my dynamically added row and column styles were not taking effect. I usually consider SuspendLayout()/ResumeLayout() as optimizations, but in this case, wrapping my code in them made the rows and columns behave correctly.
It's a weird design, but the
TableLayoutPanel.RowCount
property doesn't reflect the count of theRowStyles
collection, and similarly for theColumnCount
property and theColumnStyles
collection.What I've found I needed in my code was to manually update
RowCount
/ColumnCount
after making changes toRowStyles
/ColumnStyles
.Here's an example of code I've used:
Other thoughts
I've never used
DockStyle.Fill
to make a control fill a cell in the Grid; I've done this by setting theAnchors
property of the control.If you're adding a lot of controls, make sure you call
SuspendLayout
andResumeLayout
around the process, else things will run slow as the entire form is relaid after each control is added.Here's my code for adding a new row to a two-column TableLayoutColumn:
The label control goes in the left column and the value control goes in the right column. The controls are generally of type Label and have their AutoSize property set to true.
I don't think it matters too much, but for reference, here is the designer code that sets up detailTable:
This all works just fine. You should be aware that there appear to be some problems with disposing controls from a TableLayoutPanel dynamically using the Controls property (at least in some versions of the framework). If you need to remove controls, I suggest disposing the entire TableLayoutPanel and creating a new one.
Create a table layout panel with two columns in your form and name it
tlpFields
.Then, simply add new control to table layout panel (in this case I added 5 labels in column-1 and 5 textboxes in column-2).
Finally, run the code.
This works perfectly for adding rows and controls in a TableLayoutPanel.
Define a blank Tablelayoutpanel with 3 columns in the design page
Create a button btnAddRow to add rows on each click
I just did this last week. Set the
GrowStyle
on theTableLayoutPanel
toAddRows
orAddColumns
, then your code should work:Here is some working code that seems similar to what you are doing:
The
TableLayoutPanel
always gives me fits with size. In my example above, I'm filing an address card that might grow or shrink depending on the account having an address line two, or a country. Because the last row, or column, of the table layout panel will stretch, I throw the empty label in there to force a new empty row, then everything lines up nicely.Here is the designer code so you can see the table I start with: