C# DataGridView Check if empty

You can find out if it is empty by checking the number of rows in the DataGridView. If myDataGridView.Rows.Count == 0 then your DataGridView is empty.


The DGV.Rows.Count method of checking if DGV is empty does not work if the option AllowUserToAddRows is set to true.

You have to disable AllowUserToAddRows = false then check for empty like this:

if (dataGridView1.Rows != null && dataGridView1.Rows.Count != 0)

//this gives rows count=1

if (dataGridView1.Rows.Count != 0 && dataGridView1.Rows != null)

//so finally I modifieded code as below and it works for me

if(dataGridView1.Rows.Count>1 && dataGridView1.Rows != null)