views:

39

answers:

1

I'm adding rows in the following manner:

Table.RowCount = Table.RowCount ++;
Table.RowStyles.Add(new RowStyle(System.Windows.Forms.SizeType.AutoSize));

How can I add a Label to each cell in this newly created row?

+1  A: 

Use the TableLayoutControlCollection.Add Method. There's an overload that allows you to specify the row and column where the new Label will be added.

This snippet adds a row to a TLP and then adds a new Label to the first column of this new row:

    Label label = new Label();
    label.Name = "MyNewLabel";
    label.Text = "Added in my test";
    tableLayoutPanel1.RowCount++;
    tableLayoutPanel1.RowStyles.Add(new RowStyle());
    tableLayoutPanel1.Controls.Add(label, 0, tableLayoutPanel1.RowCount - 1);
Jay Riggs
How can I name the label when adding it?
Soo
@Soo: Yes you can. I restructured the code so it's a little clearer how the Label is created.
Jay Riggs
I see, thanks a lot Jay!
Soo
I'm using the following code, but the cells don't autosize around the labels... tableLayoutPanel1.RowStyles.Add(new RowStyle(System.Windows.Forms.SizeType.AutoSize));
Soo
@Soo: hmmm. I couldn't get it to use an AutoSize SizeType on RowStyles on programmatically added rows either. I [researched it a bit](https://connect.microsoft.com/VisualStudio/feedback/details/122017/tablelayoutpanel-rows-and-rowstyles-do-not-correspond and I wonder it it's a bug.
Jay Riggs
Jay, the only other solution I can think of is have a BUNCH of rows set up with labels, and programmatically delete the ones I don't need. Obviously this will take a lot of work, and will be limited by the number of rows I originally put in, but I can't really think of anything else. Do you have any ideas?
Soo