title: Determine number of rows in DataGridView except of new row
body: <p>How can I accuretely determine number of rows in DataGridView <em>except of new row</em> (the row with asterisk)?</p>
  
  <p><strong>If I start typing into new row and then cancel it by pressing <kbd>Esc</kbd></strong>, another new row created in the meantime disappears and original row becomes new row again. But it no longer has its <code>IsNewRow</code> property set to <code>true</code>. And <code>DataGridView.NewRowIndex</code> is suddenly set to <code>-1</code>.</p>
  
  <p><strong>How I can correctly count rows except the new row including the above special case?</strong> (<a href="http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGridViewMethods.cs,5396" rel="nofollow">Temporary reset of <code>NewRowIndex</code> to <code>-1</code> (also affecting <code>IsNewRow</code>) is a "feature" of DataGridView.</a>)</p>
  
  <p>(C# or VB – whatever you prefer.)</p>
  
  <hr>
  
  <p>For those who were unable to find the special case above –<br>
  just handle <code>DataGridView.UserDeletedRow</code> and there it is:</p>
  
   
  
  <pre class="lang-vb prettyprint-override"><code>Sub DataGridView1_UserDeletedRow1(sender As Object, e As EventArgs) _
          Handles DataGridView1.UserDeletedRow
  
      ' reveal problem with DataGridView1.NewRowIndex
      Debug.Print($"NewRowIndex={DataGridView1.NewRowIndex}")
  
      ' reveal problem with row.IsNewRow
      For Each row As DataGridViewRow In DataGridView1.Rows
          Debug.Print($"Row {row.Index}: IsNewRow={row.IsNewRow}")
      Next
  End Sub
  </code></pre>
  
  <p>Steps:</p>
  
  <ol>
  <li>Create WinForms VB project.</li>
  <li>Add <code>DataGridView1</code>.</li>
  <li>Add 2 text columns into the <code>DataGridView1</code>.</li>
  <li>Add the above event handler.</li>
  <li>Launch.</li>
  <li>Focus first column and type <code>a</code>.</li>
  <li>Immediately quit edit mode by pressing <kbd>Esc</kbd>.</li>
  <li>See the result. The above handler prints <code>NewRowIndex=-1</code>, but it should print number <code>&gt;= 0</code>. Last <code>IsNewRow</code> value is <code>false</code>, but it should be <code>true</code>.</li>
  </ol>