Dynamically add row to TableLayout Panel using C# and VB.Net in Windows Application

Last Reply 3 months ago By dharmendr

Posted 3 months ago

How to dynamically add row to tablelayout panel winforms

I have list of items

Appel

Orange

Mango

Tomato

Coke

Coffee

Water

Grapes

Papaya

Watermelon 

Now I want add this items in POS system in tablelayout panel

This panel having 7 columns

now I want dynamically add 2 rows as the items are more than 7 and single row I can display only 7 itmes

Apple Orange Mango Tomato Coke Coffee Water
Grapes Papaya watermelon        
Posted 3 months ago Modified on 2 months ago

Hi satabeach,

Refer below sample code.

C#

private void Form1_Load(object sender, EventArgs e)
{
    List<string> fruits = new List<string>();
    fruits.Add("Appel");
    fruits.Add("Orange");
    fruits.Add("Mango");
    fruits.Add("Tomato");
    fruits.Add("Coke");
    fruits.Add("Coffee");
    fruits.Add("Water");
    fruits.Add("Grapes");
    fruits.Add("Papaya");
    fruits.Add("Watermelon");

    TableLayoutPanel panel = new TableLayoutPanel();
    panel.AutoSize = true;
    panel.ColumnCount = 7;
    panel.RowCount = 1;
    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60F));
    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60F));
    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60F));
    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60F));
    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60F));
    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60F));
    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60F));
    panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F));
    panel.Controls.Add(new Label() { Text = "Fruit 1" }, 0, 0);
    panel.Controls.Add(new Label() { Text = "Fruit 2" }, 1, 0);
    panel.Controls.Add(new Label() { Text = "Fruit 3" }, 2, 0);
    panel.Controls.Add(new Label() { Text = "Fruit 4" }, 3, 0);
    panel.Controls.Add(new Label() { Text = "Fruit 5" }, 4, 0);
    panel.Controls.Add(new Label() { Text = "Fruit 6" }, 5, 0);
    panel.Controls.Add(new Label() { Text = "Fruit 7" }, 6, 0);

    int totalRow = fruits.Count() % 7 == 0 ? fruits.Count() / 7 : fruits.Count() / 7 + 1;
    int k = 0;
    for (int i = 0; i < totalRow; i++)
    {
        panel.RowCount = panel.RowCount + 1;
        for (int j = 0; j <= 6; j++)
        {
            if (fruits.Count() - 1 >= k)
            {
                panel.Controls.Add(new Label() { Text = fruits[k] }, j, panel.RowCount - 1);
                k++;
            }
        }
    }
    Controls.Add(panel);
}

VB.Net

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim fruits As List(Of String) = New List(Of String)()
    fruits.Add("Appel")
    fruits.Add("Orange")
    fruits.Add("Mango")
    fruits.Add("Tomato")
    fruits.Add("Coke")
    fruits.Add("Coffee")
    fruits.Add("Water")
    fruits.Add("Grapes")
    fruits.Add("Papaya")
    fruits.Add("Watermelon")
    Dim panel As TableLayoutPanel = New TableLayoutPanel()
    panel.AutoSize = True
    panel.ColumnCount = 7
    panel.RowCount = 1
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 60.0F))
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 60.0F))
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 60.0F))
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 60.0F))
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 60.0F))
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 60.0F))
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 60.0F))
    panel.RowStyles.Add(New RowStyle(SizeType.Absolute, 20.0F))
    panel.Controls.Add(New Label() With {.Text = "Fruit 1"}, 0, 0)
    panel.Controls.Add(New Label() With {.Text = "Fruit 2"}, 1, 0)
    panel.Controls.Add(New Label() With {.Text = "Fruit 3"}, 2, 0)
    panel.Controls.Add(New Label() With {.Text = "Fruit 4"}, 3, 0)
    panel.Controls.Add(New Label() With {.Text = "Fruit 5"}, 4, 0)
    panel.Controls.Add(New Label() With {.Text = "Fruit 6"}, 5, 0)
    panel.Controls.Add(New Label() With {.Text = "Fruit 7"}, 6, 0)
    Dim totalRow As Integer = If(fruits.Count() Mod 7 = 0, fruits.Count() / 7, fruits.Count() / 7 + 1)
    Dim k As Integer = 0
    For i As Integer = 0 To totalRow - 1
        panel.RowCount = panel.RowCount + 1
        For j As Integer = 0 To 6
            If fruits.Count() - 1 >= k Then
                panel.Controls.Add(New Label() With {.Text = fruits(k)}, j, panel.RowCount - 1)
                k += 1
            End If
        Next
    Next

    Controls.Add(panel)
End Sub

Screenshot