Search code examples
c#user-interfacedesigner

C# TableLayoutPanel first column is at wrong place


So i try to create TableLayoutPanel with 3 columns and 5 rows. When i run this code the table look like this:

https://www.dropbox.com/s/op7mdo60g4tskx4/table.PNG?dl=0

First label is not in the right place. So that is the problem. How can I fix it?

//function that creates new TableLayoutPanel and fill it with labels
 public void makeTable()
    {
        TableLayoutPanel panel = new TableLayoutPanel();
        panel.Top = 100;
        panel.Left = 30;
        panel.ColumnCount = 3;
        panel.Width = 690;
        panel.Height = 275;
        panel.RowCount = 1;
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
        panel.Controls.Add(new Label() { Text = 0 + "" }, 1, 0);
        panel.Controls.Add(new Label() { Text = 0 + "" }, 2, 0);
        panel.Controls.Add(new Label() { Text = 0 + "" }, 3, 0);

        //loop that creates the requiret amount of rows in table
        int i = 0;
        while (i < 4)
        {
            panel.RowCount++;
            panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
            panel.Controls.Add(new Label() { Text = i+1 + "" }, 1, panel.RowCount - 1);
            panel.Controls.Add(new Label() { Text = i+1 + "" }, 2, panel.RowCount - 1);
            panel.Controls.Add(new Label() { Text = i+1 + "" }, 3, panel.RowCount - 1);
            i++;
        }
        this.Controls.Add(panel);
    }

Solution

  • here is the fix. column index starts from 0 while you are starting from 1.

     panel.Controls.Add(new Label() { Text = 01 + "" }, 0, 0);
     panel.Controls.Add(new Label() { Text = 02 + "" }, 1, 0);
     panel.Controls.Add(new Label() { Text = 03 + "" }, 2, 0);
    

    complete fix

     TableLayoutPanel panel = new TableLayoutPanel();
            panel.Top = 100;
            panel.Left = 30;
            panel.ColumnCount = 3;
            panel.Width = 690;
            panel.Height = 275;
            panel.RowCount = 1;
            panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
            panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
            panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
            panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
            panel.Controls.Add(new Label() { Text = 01 + "" }, 0, 0);
            panel.Controls.Add(new Label() { Text = 02 + "" }, 1, 0);
            panel.Controls.Add(new Label() { Text = 03 + "" }, 2, 0);
    
            //loop that creates the requiret amount of rows in table
            int i = 0;
            while (i < 4)
            {
                panel.RowCount++;
                panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
                panel.Controls.Add(new Label() { Text = i + 1 + "" }, 0, panel.RowCount - 1);
                panel.Controls.Add(new Label() { Text = i + 1 + "" }, 1, panel.RowCount - 1);
                panel.Controls.Add(new Label() { Text = i + 1 + "" }, 2, panel.RowCount - 1);
                i++;
            }
            this.Controls.Add(panel);
    

    if you imagine your table layout is 3,3 grid. then 0,0 will refer first row and first column enter image description here