16

I have a datagridview which gets filled with data returned from a linq query. If the query returns no results I want to display a messagebox. Is there a way of checking to see if the datagridview is empty?

Regards

1
  • Is this ASP .Net or Windows Forms? Commented Jun 9, 2009 at 21:29

7 Answers 7

43

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't always work as stated in the answer below by Ashraf Abusada
3

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)

Comments

1

Based on the Linq results, you can hide the datagridview and show some other control (like a Literal or something) that shows the message. If you want some sort of messagebox popup, you'd need to throw some JavaScript in there.

1 Comment

can't see why jscript would be needed at all, ever
1

A lot of answers here have a reference to Rows.Count. Normally, that does not pose a problem and it would in most cases be an overkill to do what I am about to suggest.

But for reasons mentioned in this document it may not be a good idea to call Rows.Count if the DataGridView frequently has a lot of data (>~ 5000 cells in the memory profiling I did to validate that article a while back).

Avoid using the Count property of the System.Windows.Forms.DataGridViewSelectedCellCollection to determine the number of selected cells. Instead, use the DataGridView.GetCellCount method and pass in the DataGridViewElementStates.Selected value. Similarly, use the DataGridViewRowCollection.GetRowCount and DataGridViewColumnCollection.GetColumnCount methods to determine the number of selected elements, rather than accessing the selected row and column collections.

In such cases you can use

myDataGridView1.Rows.GetRowCount(.) == 0

If you are not dealing with fast changing data or a huge amount of data (or worse, huge amount of fast changing data) then simply use Rows.Count --it does not hurt that much.

Comments

1

//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)

Comments

0

You can check the Rows.Count property of the datagridview.

Although you might also want to look into the EmptyDataText property of the DataGridView. It might save you showing a messagebox.

Comments

-1

This should do it:

dataGridView1.RowCount == 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.