Add rows to a TableLayoutPanel at run-time?
OK, I know how to add rows at design-time, but what I need to do is add rows at run-time. So far I have code that looks like this:TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(SizeType.Absolute, 20))
I have this code behind a button, which when clicked executes this code. It works fine the first time the button is pressed - adding a new row. However all subsequent presses do nothing? The code executes, but no more rows are added to the TableLayoutPanel.
Does anyone have any ideas as to what I might be doing wrong? Thanks.
Your code worked fine with me.....
I guess you have changed the rowcount and collumncount properties first
Are you using the beta version of VS?
Had the same problem.
But you said that no rows where added, in my case more and more rows where added.
You need to clear the controls that you've added in the table.
Something like:
tblData.Controls.Clear();
Then set the RowCount again to the new number of rows.
Then add you're controls again.
Below a sample which I used to test:
Code Snippet
private void fillData()
{
//Clear all the controls
tblData.Controls.Clear();
//Set the new count
tblData.RowCount =
Convert.ToInt16(textBox1.Text) ; for (int i = 0; i < tblData.RowCount; i++) {
tblData.RowStyles.Add(
new RowStyle(SizeType.Percent,100)); Label lblLabel = new Label(); //This is just to have text that is smaller then the rest. //Just to see how the table reacts. if ((i > 3) & (i < 6)) lblLabel.Text =
"Label: " + i.ToString(); else lblLabel.Text =
"Label control: " + i.ToString(); lblLabel.Font =
new Font("Verdana", 8, FontStyle.Bold | FontStyle.Regular); lblLabel.AutoSize =
true; Label lblData = new Label(); lblData.Text =
"Data for entity (" + i.ToString() + ")"; lblData.Font =
new Font("Verdana", 8, FontStyle.Regular); lblData.AutoSize =
true; tblData.Controls.Add(lblLabel,
0, i); tblData.Controls.Add(lblData,
1, i);
}
}
This method is executed when a button is clicked.
tblData is a TableLayoutPanel with one row and two colums set in design time. (First column fixed size, second column SizeType = Percent.