Q:

TableLayoutPanel scrollbars bugged at specific amount of rows?

I use a TableLayouPanel, programmatically add rows of 50px height each. TLP y-size is 217 px, so adding 5 rows results in a vertical scrollbar but also a horizontal scrollbar. 
When I add another row - still vertical scrollbar - the horizontal scrollbar disappears.
Tried to change the TLP's size to anything between 200 and 250, stays the same. 
How do I remove this behaviour as any number of rows but 5 works fine?
Code from designer and adding rows:
System.Windows.Forms.TableLayoutPanel tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.ColumnCount = 3;
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
tableLayoutPanel1.RowCount = 1;
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel1.Size = new System.Drawing.Size(709, 217);

tableLayoutPanel1.RowStyles.RemoveAt(0);
for (int i = tableLayoutPanel1.Controls.Count - 1; i >= 0; i--) {
    tableLayoutPanel1.Controls[i].Dispose();
}
for(int i = 0; i < 5; i++) {
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
    tableLayoutPanel1.Controls.Add(new Control(), 0, i);
    tableLayoutPanel1.Controls.Add(new Control(), 1, i);
    tableLayoutPanel1.Controls.Add(new Control(), 2, i);
}
if (!tableLayoutPanel1.VerticalScroll.Visible){ //false for 5
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
}

tl;dr why is there a horizontal scrollbar with 5 rows and how to make content fit outer control again so there is none?

Thought of fixing this by adding another row and deleting it again, but since TableLayoutPanel doesn't give a fck, the last row automatically takes up the "remaining" space of the removed row...atleast that's what my current progress looks like, not sure if I should change everything to DataGridView

A:

I tried a lot of different things and found a solution working for me:

tableLayoutPanel1.RowStyles.RemoveAt(0);
for (int i = tableLayoutPanel1.Controls.Count - 1; i >= 0; i--) {
    tableLayoutPanel1.Controls[i].Dispose();
}

became
int i;  
while ((i = tableLayoutPanel1.Controls.Count) >= 2) {
    tableLayoutPanel1.Controls[--i].Dispose();      
    tableLayoutPanel1.Controls[--i].Dispose();      
    tableLayoutPanel1.Controls[--i].Dispose();  
}   
while (tableLayoutPanel1.RowStyles.Count > 0) {         
    tableLayoutPanel1.RowStyles.RemoveAt(tableLayoutPanel1.RowStyles.Count - 1);
}

and (empty row with variable size to keep the last row's size if there are less rows than space before a scrollbar is necessary)

if (!tableLayoutPanel1.VerticalScroll.Visible){ //false for 5
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
}

became
tableLayoutPanel1.Update(); //not sure if necessary yet
if (!tableLayoutPanel1.VerticalScroll.Visible) {
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
} else if (tableLayoutPanel1.HorizontalScroll.Visible) {
    if (tableLayoutPanel1.RowStyles.Count != inp.Count) {
        tableLayoutPanel1.GetControlFromPosition(0, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(1, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(2, inp.Count).Dispose();
        tableLayoutPanel1.RowStyles.RemoveAt(inp.Count);
    } else {
        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
        tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
        tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
        tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
        tableLayoutPanel1.GetControlFromPosition(0, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(1, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(2, inp.Count).Dispose();
        tableLayoutPanel1.RowStyles.RemoveAt(inp.Count);
    }
}

For anyone wondering: inp.Count is the amount of actually added non-empty rows.
It might be possible to reformat and shorten this, as it looks redundant, but I worked long enough to get it to work at all, should be fine for now.

