在TableLayoutPanel中动态添加行

46

我想在Windows Form中使用C#动态按行将这些条目添加到TableLayoutPanel中

我应该如何做?

4个回答

78

请尝试以下代码:

// TableLayoutPanel Initialization
TableLayoutPanel panel = new TableLayoutPanel();
panel.ColumnCount = 3;
panel.RowCount = 1;
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40F));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 50F));
panel.Controls.Add(new Label() { Text = "Address" }, 1, 0);
panel.Controls.Add(new Label() { Text = "Contact No" }, 2, 0);
panel.Controls.Add(new Label() { Text = "Email ID" }, 3, 0);

// For Add New Row (Loop this code for add multiple rows)
panel.RowCount = panel.RowCount + 1;
panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 50F));
panel.Controls.Add(new Label() { Text = "Street, City, State" }, 1, panel.RowCount-1);
panel.Controls.Add(new Label() { Text = "888888888888" }, 2, panel.RowCount-1);
panel.Controls.Add(new Label() { Text = "xxxxxxx@gmail.com" }, 3, panel.RowCount-1);

1
对于可能遇到此问题的所有人。由于我在设计器中进行了更改,设计器生成的代码添加了多个RowStyles,即使它只有一行。这会影响像这样添加行,因为新的RowStyles没有被正确使用。如果您遇到问题,请检查设计器生成的代码,确保只有与行数相同的RowStyles。 - sbecker

26
我知道这个问题很老,但有人可能会发现以下内容有用:
首先,请注意petchirajan的答案很好,但是如果您至少有一行现有行(例如标题),并且希望继续使用视觉编辑器设置的高度来继续列表,而无需修改代码,则可以使用以下方法:
private void AddItem(string address, string contactNum, string email )
    {
        //get a reference to the previous existent 
        RowStyle temp = panel.RowStyles[panel.RowCount - 1];
        //increase panel rows count by one
        panel.RowCount++;
        //add a new RowStyle as a copy of the previous one
        panel.RowStyles.Add(new RowStyle(temp.SizeType, temp.Height));
        //add your three controls
        panel.Controls.Add(new Label() {Text = address}, 0, panel.RowCount - 1);
        panel.Controls.Add(new Label() { Text = contactNum }, 1, panel.RowCount - 1);
        panel.Controls.Add(new Label() { Text = email }, 2, panel.RowCount - 1);
    }

如果您喜欢一种适用于通用表格的方法:

private void AddRowToPanel(TableLayoutPanel panel, string[] rowElements)
    {
        if (panel.ColumnCount != rowElements.Length)
            throw new Exception("Elements number doesn't match!");
        //get a reference to the previous existent row
        RowStyle temp = panel.RowStyles[panel.RowCount - 1];
        //increase panel rows count by one
        panel.RowCount++;
        //add a new RowStyle as a copy of the previous one
        panel.RowStyles.Add(new RowStyle(temp.SizeType, temp.Height));
        //add the control
        for (int i = 0; i < rowElements.Length; i++)
        {
            panel.Controls.Add(new Label() { Text = rowElements[i] }, i, panel.RowCount - 1);
        }
    }

您可以使用Collection而不是数组来执行此操作,方法如下:
 private void AddRowToPanel(TableLayoutPanel panel, IList<string> rowElements)
    ...

希望这能帮到你。

有没有办法在不拉伸和压缩屏幕的情况下添加滚动条?插入几行后,用户界面变得非常奇怪。 - Jamshaid K.
@CodeIt将你的TableLayoutPanel放到Panel中并将其AutoScroll = true。TableLayoutPanel在设计器中也是可滚动的。 :)记得使用定义值设置行大小,不要使用“自动”或百分比值来避免行调整大小! - tedebus

2
在 foreach 示例中:
int i=0;
foreach (KeyValuePair<string,string> kv in MyCollection)
{
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F));
    tableLayoutPanel1.Controls.Add(new Label() { Text = kv.Key }, 0, i);
    i++;
}

0
这里有几个扩展方法可以使用:
您可以通过以下方式调用它们:
TableLayoutPanel tablePanel = new TableLayoutPanel(); //Initialize and do any other construction
tablePanel.AddColumn(null, "Column1");
tablePanel.AddRow(new RowStyle() { SizeType = SizeType.Absolute, Height = 50 }, "RowData1", "RowData2", "RowData3");

public static int AddRow(this TableLayoutPanel table, RowStyle rowStyle = null, params string[] rowData)
{
    List<Label> labels = new List<Label>();
    rowData.ToList().ForEach(p => labels.Add(new Label() { Text = p }));
    return table.AddRow(rowStyle, labels.ToArray());
}

public static int AddRow(this TableLayoutPanel table, RowStyle rowStyle = null, params Control[] rowData)
{
    table.RowCount = table.RowCount + 1;

    if (rowStyle == null)
        rowStyle = new RowStyle(SizeType.AutoSize);

    table.RowStyles.Add(rowStyle);

    for (int i = 0; i < rowData.Length; i++)
    {
        if (i > table.ColumnCount - 1)
            break;

        table.Controls.Add(rowData[i], i, table.RowCount - 1);
    }

    return table.RowCount - 1;
}

public static int AddColumn(this TableLayoutPanel table, ColumnStyle columnStyle = null, params string[] columnData)
{
    List<Label> labels = new List<Label>();
    columnData.ToList().ForEach(p => labels.Add(new Label() { Text = p }));
    return table.AddColumn(columnStyle, labels.ToArray());
}

public static int AddColumn(this TableLayoutPanel table, ColumnStyle columnStyle = null, params Control[] columnData)
{
    table.ColumnCount = table.ColumnCount + 1;

    if (columnStyle == null)
        columnStyle = new ColumnStyle(SizeType.AutoSize);

    table.ColumnStyles.Add(columnStyle);

    for (int i = 0; i < columnData.Length; i++)
    {
        if (i > table.RowCount - 1)
            break;

        table.Controls.Add(columnData[i], table.ColumnCount - 1, i);
    }

    return table.ColumnCount - 1;
}

TableLayoutPanel有AddColumn方法吗? - user14967521
@biryaz 不是的,这些是扩展方法。https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods - derekantrican

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接