繁体   English   中英

在 TableLayoutPanel 中动态添加行

[英]Add Row Dynamically in TableLayoutPanel

在此处输入图片说明

我想在 c# 中的 Windows 窗体的 TableLayoutPanel 中逐行动态添加这些条目

我怎样才能做到这一点?

试试下面的代码,

// 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);

我知道这个问题很老了,但有人会发现以下内容很有用:

首先,请注意 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)
    ...

希望这可以帮助。

在 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++;
}

以下是您可以使用的几种扩展方法:

您可以通过执行以下操作来调用它们:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM