title: Programmatically scrolling datagridview by increments until it hits bottom, then scroll back to top
body: <p>I'm trying to scroll my Datagridview programmatically by increments of 2. When it reaches the bottom, I'd like to reset the scroll back to the top.</p>
  
  <p>Here is my current code:</p>
  
  <pre><code>private int scrollPosition = 0;
  
              if(scrollPosition == 0)
              {
                  // This means the scroll is at the top
                  scrollPosition+=2;
  
              }
              else if(scrollPosition &gt; 0 &amp;&amp; scrollPosition &lt; dataGridView1.RowCount-1 -2)
              {
                  // This means the scroll is somewhere in the middle
                  scrollPosition+=2;
              }
              else
              {
                  // This means the scroll is at the bottom
                  scrollPosition = 0;
  
              }
              dataGridView1.FirstDisplayedScrollingRowIndex = scrollPosition;
  </code></pre>
  
  <p>That code is inside a method that gets called every 10 seconds.</p>
  
  <p>I have tried debugging the code. Based on what I saw, the code never triggers the else statement. Hence, it does not reset the scroll back to the top. What logic did I do wrong?</p>
  
  <p>Update: My mistake, it triggers the else statement at some point. But it doesn't trigger the else statement the moment the scroll is at the bottom already. Since I have 20 entries in my Datagridview, it takes a while for the scroll position to get near that value. </p>
  
  <p>The Datagridview scroll is already at the bottom before my scrollPosition reaches a value that would trigger the else statement. </p>
  
  <p>Is there a more accurate variable I can compare my scrollPosition to? So that when my Datagridview scroll is at the bottom already, it triggers the else statement as it should at the right time. </p>