locked
Add rows to a TableLayoutPanel at run-time? RRS feed

  • Question

  • OK, I know how to add rows at design-time, but what I need to do is add rows at run-time. So far I have code that looks like this:

    TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(SizeType.Absolute, 20))

    I have this code behind a button, which when clicked executes this code. It works fine the first time the button is pressed - adding a new row. However all subsequent presses do nothing??? The code executes, but no more rows are added to the TableLayoutPanel.

    Does anyone have any ideas as to what I might be doing wrong? Thanks.

    Thursday, July 28, 2005 8:37 AM

Answers

  • Your code worked fine with me.....

    I guess you have changed the rowcount and collumncount properties first

    Are you using the beta version of VS?

     

    Thursday, January 19, 2006 3:43 AM

All replies

  • Your code worked fine with me.....

    I guess you have changed the rowcount and collumncount properties first

    Are you using the beta version of VS?

     

    Thursday, January 19, 2006 3:43 AM
  • Had the same problem.

    But you said that no rows where added, in my case more and more rows where added.

     

    You need to clear the controls that you've added in the table.

     

    Something like:

    tblData.Controls.Clear();

     

    Then set the RowCount again to the new number of rows.

    Then add you're controls again.

     

    Below a sample which I used to test:

     

    Code Snippet

    private void fillData()

    {

    //Clear all the controls

    tblData.Controls.Clear();

    //Set the new count

    tblData.RowCount = Convert.ToInt16(textBox1.Text) ;

    for (int i = 0; i < tblData.RowCount; i++)

    {

    tblData.RowStyles.Add(new RowStyle(SizeType.Percent,100));

    Label lblLabel = new Label();

    //This is just to have text that is smaller then the rest.

    //Just to see how the table reacts.

    if ((i > 3) & (i < 6))

    lblLabel.Text = "Label: " + i.ToString();

    else

    lblLabel.Text = "Label control: " + i.ToString();

    lblLabel.Font = new Font("Verdana", 8, FontStyle.Bold | FontStyle.Regular);

    lblLabel.AutoSize = true;

    Label lblData = new Label();

    lblData.Text = "Data for entity (" + i.ToString() + ")";

    lblData.Font = new Font("Verdana", 8, FontStyle.Regular);

    lblData.AutoSize = true;

    tblData.Controls.Add(lblLabel, 0, i);

    tblData.Controls.Add(lblData, 1, i);

    }

    }

     

    This method is executed when a button is clicked.

    tblData is a TableLayoutPanel with one row and two colums set in design time. (First column fixed size, second column SizeType = Percent.

     

    Wednesday, July 4, 2007 12:17 PM
  •  

    Had the same problem.  Make sure you do set AutoSize...

     

    tableLayoutPanel.AutoSize = True

    Tuesday, November 13, 2007 7:36 PM
  • I think, tablelayoutpanel changing rowstyles after you add a new row, I had the same problem I fixed it by adding this code after adding controls;

     

    for (int i = 0; i < tableLayoutPanel2.RowCount; i++)

    {

    tableLayoutPanel2.RowStylesIdea.SizeType = SizeType.Percent;

    tableLayoutPanel2.RowStylesIdea.Height = 100;

    }

    Friday, November 16, 2007 1:50 PM
  • Thank you for that code.  It is, for the most part, helpful.  The only thing that I would suggest adding right after tblData.Controls.Clear() is:

    tblData.RowStyles.Clear()

    this will get rid of any formatting you may have created in the visual editor or, perhaps, previous styling you have applied to the row at run time.

    Just a Suggestion...
    Thursday, January 10, 2008 5:48 PM