C# 使用TableLayoutPanel添加列后添加行
我正在尝试向TableLayoutPanel动态添加列和行。到目前为止,我有以下代码:C# 使用TableLayoutPanel添加列后添加行,c#,.net,winforms,C#,.net,Winforms,我正在尝试向TableLayoutPanel动态添加列和行。到目前为止,我有以下代码: l = new Label(); l.Text = "" + headers[headers.Count-1]; ColumnStyle cStyle = new ColumnStyle(SizeType.AutoSize); theTable.ColumnStyles.Add(cStyle); theTable.Controls.Add(l, colCount, 0); colCount++;
l = new Label();
l.Text = "" + headers[headers.Count-1];
ColumnStyle cStyle = new ColumnStyle(SizeType.AutoSize);
theTable.ColumnStyles.Add(cStyle);
theTable.Controls.Add(l, colCount, 0);
colCount++;
这将添加我需要的所有列。然后,我尝试使用以下命令切换到添加行:
theTable.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
但这不起作用。它取而代之的是我添加的列,并使它们成为行。有没有一种方法可以动态创建列,然后动态创建行
谢谢我无法重现您的问题:
private void AddTLP()
{
List<string> headers = new List<string>();
headers.Add("Column 1");
headers.Add("Column 2");
headers.Add("Column 3");
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.Size = new Size(356, 120);
tlp.BackColor = Color.Gray;
for (int i = 0; i < headers.Count; i++)
{
Label l = new Label();
l.Text = headers[i].ToString();
ColumnStyle cStyle = new ColumnStyle(SizeType.AutoSize);
tlp.ColumnStyles.Add(cStyle);
tlp.Controls.Add(l, i, 0);
}
tlp.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
// Add controls to test growth:
tlp.Controls.Add(new Button(), 0, 1);
tlp.Controls.Add(new TextBox(), 1, 2);
this.Controls.Add(tlp);
}
private void AddTLP()
{
列表标题=新列表();
标题。添加(“第1列”);
标题。添加(“第2列”);
标题。添加(“第3列”);
TableLayoutPanel tlp=新的TableLayoutPanel();
tlp尺寸=新尺寸(356120);
tlp.BackColor=颜色。灰色;
对于(int i=0;i
一定有一些代码是您没有显示的,导致了问题。我无法重现您的问题:
private void AddTLP()
{
List<string> headers = new List<string>();
headers.Add("Column 1");
headers.Add("Column 2");
headers.Add("Column 3");
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.Size = new Size(356, 120);
tlp.BackColor = Color.Gray;
for (int i = 0; i < headers.Count; i++)
{
Label l = new Label();
l.Text = headers[i].ToString();
ColumnStyle cStyle = new ColumnStyle(SizeType.AutoSize);
tlp.ColumnStyles.Add(cStyle);
tlp.Controls.Add(l, i, 0);
}
tlp.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
// Add controls to test growth:
tlp.Controls.Add(new Button(), 0, 1);
tlp.Controls.Add(new TextBox(), 1, 2);
this.Controls.Add(tlp);
}
private void AddTLP()
{
列表标题=新列表();
标题。添加(“第1列”);
标题。添加(“第2列”);
标题。添加(“第3列”);
TableLayoutPanel tlp=新的TableLayoutPanel();
tlp尺寸=新尺寸(356120);
tlp.BackColor=颜色。灰色;
对于(int i=0;i
一定有一些代码没有显示,这是导致问题的原因