Add Row Dynamically in TableLayoutPanel

2019-03-11 16:58发布

enter image description here

I want to add these entries dynamically row by row in TableLayoutPanel in Windows Form in c#

How can I do that?

3条回答
等我变得足够好
2楼-- · 2019-03-11 17:04

Try the below code,

// 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);
查看更多
该账号已被封号
3楼-- · 2019-03-11 17:17

in foreach example:

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++;
}
查看更多
家丑人穷心不美
4楼-- · 2019-03-11 17:18

I know that this question is very old, but someone could take this as useful thing.

First of all note that petchirajan answer is good, but if you have at least one existent row (titles, for example) and you want to continue the list using height set using visual editor, without modify the code, you can use this:

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

If you prefer generic method for a generic table:

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

You can do this also using a Collection instead of an array using

 private void AddRowToPanel(TableLayoutPanel panel, IList<string> rowElements)
    ...

Hope it helps.

查看更多
登录 后发表回答