C# DataGridView Check if empty

后端 未结 7 1838
故里飘歌
故里飘歌 2020-12-17 15:01

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 s

7条回答
  •  忘掉有多难
    2020-12-17 15:42

    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.

提交回复
热议问题