TableLayoutPanel Spacing Issue

536 views Asked by At

I adding labels to a tablelayoutpanel from an array. The labels add no problem, but their is a huge gap between each of the labels. Did I code something incorrectly? This is my array, and adding the labels. The tablelayoutpanel is added from the GUI and is named tablelayoutpanel1

while (dr.Read())
{
labelsToAdd.Add(dr[0].ToString());
}
dr.Close();
foreach (string label in labelsToAdd)
{
Label lbl = new Label();
lbl.Name = "lbl_" + index;
lbl.Text = label;
lbl.AutoSize = true;
tableLayoutPanel1.Controls.Add(lbl, 0, rowIndex);
rowIndex++;
}

So they will add like this with all the whitespace in between them

label1





label2

Visual Sample - enter image description here

2

There are 2 answers

0
LarsTech On BEST ANSWER

From your comment:

only the 1st two display like that. The rest display as desired.

So it looks like you should clear the rows first since your GrowStyle = AddRows:

tableLayoutPanel1.RowStyles.Clear();
foreach (string label in labelsToAdd)
{
  Label lbl = new Label();
  // etc...
4
MikeG On

Try setting the row height:

tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 30))