简体   繁体   English

C#DataGridView检查是否为空

[英]C# DataGridView Check if empty

I have a datagridview which gets filled with data returned from a linq query. 我有一个datagridview,它填充了linq查询返回的数据。 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? 有没有办法检查datagridview是否为空?

Regards 问候

You can find out if it is empty by checking the number of rows in the DataGridView. 您可以通过检查DataGridView中的行数来确定它是否为空。 If myDataGridView.Rows.Count == 0 then your DataGridView is empty. 如果myDataGridView.Rows.Count == 0那么您的DataGridView为空。

The DGV.Rows.Count method of checking if DGV is empty does not work if the option AllowUserToAddRows is set to true. 如果选项AllowUserToAddRows设置为true,则检查DGV是否为空的DGV.Rows.Count方法不起作用。

You have to disable AllowUserToAddRows = false then check for empty like this: 你必须禁用AllowUserToAddRows = false然后像这样检查空:

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

//this gives rows count=1 //这给行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)

A lot of answers here have a reference to Rows.Count . 这里有很多答案都提到了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). 但是由于本文档中提到的原因,如果DataGridView经常拥有大量数据(在内存分析中有>~ 5000单元用于验证该文章一段时间),则调用Rows.Count可能不是一个好主意。

Avoid using the Count property of the System.Windows.Forms.DataGridViewSelectedCellCollection to determine the number of selected cells. 避免使用System.Windows.Forms.DataGridViewSelectedCellCollectionCount属性来确定所选单元格的数量。 Instead, use the DataGridView.GetCellCount method and pass in the DataGridViewElementStates.Selected value. 而是,使用DataGridView.GetCellCount方法并传入DataGridViewElementStates.Selected值。 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. 同样,使用DataGridViewRowCollection.GetRowCountDataGridViewColumnCollection.GetColumnCount方法来确定所选元素的数量,而不是访问选定的行和列集合。

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. 如果您不处理快速变化的数据或大量数据(或更糟糕的是,大量快速变化的数据),那么只需使用Rows.Count - 它不会伤害那么多。

Based on the Linq results, you can hide the datagridview and show some other control (like a Literal or something) that shows the message. 根据Linq结果,您可以隐藏datagridview并显示一些显示消息的其他控件(如Literal或其他内容)。 If you want some sort of messagebox popup, you'd need to throw some JavaScript in there. 如果你想要某种消息框弹出窗口,你需要在那里抛出一些JavaScript。

这应该这样做:

dataGridView1.RowCount == 0

You can check the Rows.Count property of the datagridview. 您可以检查datagridview的Rows.Count属性。

Although you might also want to look into the EmptyDataText property of the DataGridView. 虽然您可能还想查看DataGridView的EmptyDataText属性。 It might save you showing a messagebox. 它可能会保存您显示消息框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM