It's a weird design, but the TableLayoutPanel.RowCount
property doesn't reflect the count of the RowStyles
collection, and similarly for the ColumnCount
property and the ColumnStyles
collection.
What I've found I needed in my code was to manually update RowCount
/ColumnCount
after making changes to RowStyles
/ColumnStyles
.
Here's an example of code I've used:
/// <summary>
/// Add a new row to our grid.
/// </summary>
/// The row should autosize to match whatever is placed within.
/// <returns>Index of new row.</returns>
public int AddAutoSizeRow()
{
Panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
Panel.RowCount = Panel.RowStyles.Count;
mCurrentRow = Panel.RowCount - 1;
return mCurrentRow;
}
Other thoughts
I've never used DockStyle.Fill
to make a control fill a cell in the Grid; I've done this by setting the Anchors
property of the control.
If you're adding a lot of controls, make sure you call SuspendLayout
and ResumeLayout
around the process, else things will run slow as the entire form is relaid after each control is added.