Jamie Jamie - 1 month ago 15
C# Question

Checking if DataGridView is empty

I want to check if my

DataGridView
is empty. Of course I searched on internet and they are saying you have to do it like this:

if (datagridview.Rows.Count < 0) {
//my messagebox
}


But when I have a empty table and say :

int test = datagridview.Rows.Count;
MessageBox.Show(test.ToString());


The
MessageBox
is telling me
1
??

How is that possible, now I can't check if a table is empty?

Answer

Set AllowUserToAddRows to False can fix your problem:

dataGridView.AllowUserToAddRows = false;

Then:

int test = datagridview.Rows.Count;
MessageBox.Show(test.ToString());//Return 0