title: How can I prevent a DGV from hiding the top row?
body: <p>My DGV is populated with values entered by the user (not data-bound). The row count is dynamic. Even with the default five rows, though, tabbing from cell to cell and from row to row causes the first row (row 0) to disappear "up and under" the DGV when I get to the last (5th) row / row 4.</p>
  
  <p>I want the 5 (or however many) rows to always stay in the same spot, rather than to move based on where the cursor is.</p>
  
  <p>I don't know why this is the default behavior in this scenario, and am curious about that, but mostly just need to know how to prevent this bizarreness.</p>
  
  <p>When users add additional rows, I calculate the heights of each row like so:</p>
  
  <pre><code>dataGridViewPlatypi.Rows.Add();
  int newRowHeight = dataGridViewPlatypi.Height / dataGridViewPlatypi.RowCount;
  for (int i = 0; i &lt; dataGridViewPlatypi.RowCount; i++)
  {
      dataGridViewPlatypi.Rows[i].Height = newRowHeight;
  }
  </code></pre>
  
  <p>...this inexact math can lead to some "extra space" at the bottom of the grid when the division has a remainder/modulo, such as: When there are six rows, the height of each row is 26 and leaves 4 pixels over on the bottom of the grid -- but again, this unnerving behavior even occurs when the default 5 rows are ensconced in the 160 pixel-high DGV (32 pixels per row).</p>
  
  <p>I don't know if I need to set some property or write some code for the DGV, a row, a cell, or...???</p>
  
  <h2>UPDATE</h2>
  
  <p>I've found that if I add 2 to the DGV's height at design time (162 instead of 160, so that each row is 32 and there are 2 pixels "for the DGV" left over, it works with the default 5 rows.</p>
  
  <p>HOWEVER, trying to get it to adjust when the user adds additional rows is proving problematic. The following kludgy code does NOT work:</p>
  
  <pre><code>// Adding one extra pixel is not enough; 2 is the least that can be added to prevent the last phantom grey row from appearing
  const int BASE_GRID_HEIGHT_SHIM = 2;
  const int DEFAULT_ROW_COUNT = 5;
  
  dataGridViewPlatypi.Rows.Add();
  
  int shimAdjustment = dataGridViewPlatypi.RowCount - DEFAULT_ROW_COUNT;
  int newRowHeight = dataGridViewPlatypi.Height / dataGridViewPlatypi.RowCount;
  int newGridHeight = (newRowHeight * dataGridViewPlatypi.RowCount) + BASE_GRID_HEIGHT_SHIM + shimAdjustment;
  
  for (int i = 0; i &lt; dataGridViewPlatypi.RowCount; i++)
  {
      dataGridViewPlatypi.Rows[i].Height = newRowHeight;
  }
  
  dataGridViewPlatypi.Size = new Size(dataGridViewPlatypi.Width, newGridHeight);
  </code></pre>
  
  <p>NOTE: The pushing up of the rows to hide row 0 and reveal the soft grey underbelly of the DGV does not occur when clicking in column 0; it is only after tabbing from one of the columns to the next column that this "up-pushing" occurs (FWIW).</p>
  
  <h2>UPDATE 2</h2>
  
  <p>This is how I applied the bountiful answer. </p>
  
  <pre><code>private void buttonAddRow_Click(object sender, EventArgs e)
  {
      AddAPlatypusRow();
      for (int i = 0; i &lt; dataGridViewPlatypi.RowCount; i++)
      {
          FreezeBand(dataGridViewPlatypi.Rows[i]);
      }
  }
  </code></pre>