46

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?

ygoe
  • 18,655
  • 23
  • 113
  • 210
akash
  • 22,664
  • 11
  • 59
  • 87

4 Answers4

77

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);
petchirajan
  • 4,152
  • 1
  • 18
  • 20
  • 1
    For everyone that might have issues with this. Due to changes I made in the designer, the designer generated code added multiple RowStyles, even though it only had one row. This screwed with adding rows like this, because the new RowStyles weren't used properly. If you have issues, check the designer generated code to make sure you there only as many RowStyles as there are rows. – sbecker Feb 20 '21 at 18:24
26

I know that this question is very old, but someone could find the following as useful:

First of all, note that petchirajan's answer is good, but if you have at least one existent row (titles, for example) and you want to continue the list using the height set using visual editor, and without having to 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 a 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 this helps.

RoastBeast
  • 1,059
  • 2
  • 22
  • 38
tedebus
  • 978
  • 13
  • 20
  • isn't there any way by which we can add a scrollbar without it's screens being stretched and pressed? UI after inserting few rows goes really weird – Jamshaid K. Jul 04 '17 at 18:27
  • @CodeIt put your TableLayoutPanel inside a Panel and set its AutoScroll = true. TableLayoutPanel will be scrollable in designer too. :) Remember to set rows size with defined values and don't use "Auto" or percentual values to avoid row resizing! – tedebus Jul 27 '17 at 08:59
2

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++;
}
T.Todua
  • 53,146
  • 19
  • 236
  • 237
0

Here are a couple extension methods you can use:

You can call them by doing the following:

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;
}
derekantrican
  • 1,891
  • 3
  • 27
  • 57