title: WinForms: Proper way to change number of rows in a TableLayoutPanel
body: <p>i'm trying to change the number of rows in a <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx" rel="nofollow noreferrer">TableLayoutPanel</a> programatically (sometimes it needs to be four, sometimes five, and rarely six).</p>
  
  <p>Unfortunatly changing the number of rows does not keep the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.rowstyles.aspx" rel="nofollow noreferrer"><code>RowStyles</code></a> collection in sync, so you are then not able to set the height of the newly added rows. The following test code demonstrates this fact:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      //TableLayoutPanels start with 2 rows by default.
      Debug.Assert(tableLayoutPanel1.RowStyles.Count == 
              tableLayoutPanel1.RowCount);
  
      //Cannot remove rows
      tableLayoutPanel1.RowCount = 1;
      Debug.Assert(tableLayoutPanel1.RowStyles.Count == 
              tableLayoutPanel1.RowCount);
  
  }
  </code></pre>
  
  <p>The second assertion fails.</p>
  
  <pre><code>private void button2_Click(object sender, EventArgs e)
  {
      //TableLayoutPanels start with 2 rows by default.
      Debug.Assert(tableLayoutPanel1.RowStyles.Count == 
              tableLayoutPanel1.RowCount);
  
      //Cannot add rows
      tableLayoutPanel1.RowCount = 6;
      Debug.Assert(tableLayoutPanel1.RowStyles.Count == 
              tableLayoutPanel1.RowCount);
  }
  </code></pre>
  
  <p>The second assertion fails.</p>
  
  <p>So what's the proper programatic way to set the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.rowcount.aspx" rel="nofollow noreferrer"><code>RowCount</code></a> property of a <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx" rel="nofollow noreferrer"><code>TableLayoutPanel</code></a>?</p>