我已经使用设计器添加了 TableLayoutPanel,但它不允许我以编程方式向表添加更多行。旧线程中的东西不起作用。
这是我的代码:
tableLayoutPanel1_3Table.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
for (int i = 0; i < 10; i++)
{
Label label = new Label();
label.Text = "he";
label.Dock = DockStyle.Fill;
tableLayoutPanel1_3Table.Controls.Add(label, 0, i+1); /
}
我也尝试过这个:
for (int i = 0;i < 3;i++)
{
RowStyle newRowStyle = new RowStyle(SizeType.Absolute, 200);
tableLayoutPanel1_3Table.RowCount++;
int rowIndex = tableLayoutPanel1_3Table.RowCount - 1; //I've also tried removing -1 from this
tableLayoutPanel1_3Table.RowStyles.Insert(rowIndex, newRowStyle);
tableLayoutPanel1_3Table.PerformLayout();
}
这似乎有效,但前提是我使用
RowStyles.Clear
,这是不可行的。
for(int i = 0; i < 10; i++){
tableLayoutPanel1_3Table.RowStyles.Add(new RowStyle(SizeType.Absolute, 150F));
tableLayoutPanel1_3Table.RowCount++;
Label label = new Label();
label.Text = "he";
tableLayoutPanel1_3Table.Controls.Add(label, 0, i);
}
tableLayoutPanel1_3Table.RowStyles.Clear();
我尝试下面的代码,它多次显示“He”。
TableLayoutPanel tableLayoutPanel1_3Table = new TableLayoutPanel();
tableLayoutPanel1_3Table.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
for (int i = 0; i < 10; i++)
{
Label label = new Label();
label.Text = "he";
label.Dock = DockStyle.Fill;
tableLayoutPanel1_3Table.Controls.Add(label, 0, i + 1);
}
Controls.Add(tableLayoutPanel1_3Table);