Robert Hill

Robert Hill

  • NA
  • 4
  • 0

Deleting the last row of a DataGridView

Jun 14 2008 3:00 PM

I am trying to delete the last row of a DataGridView.  When I highlight the last row and run my delete code, all of the rows are deleted, one by one.  It apperas that after the bottom row is deleted, the second from the bottom gets focus and is highlighted and is then deleted, and so on.  Here is my simple code.

int rows = this.dataGridViewData.Rows.Count - 1;
for (int i = rows; i >= 0; i--)
{
   if (this.dataGridViewData.Rows[i].Selected == true)
   {
      this.dataGridViewData.Rows.RemoveAt(i);
   }
}

If there is a more efficient way of doing this, please let me know.

Thanks...


Answers (1)