C# DataGridView 检查是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/972496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:34:16  来源:igfitidea点击:

C# DataGridView Check if empty

c#winformsdatagridview

提问by Goober

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?

我有一个 datagridview,它填充了从 linq 查询返回的数据。如果查询没有返回结果,我想显示一个消息框。有没有办法检查 datagridview 是否为空?

Regards

问候

采纳答案by Meta-Knight

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

您可以通过检查 DataGridView 中的行数来确定它是否为空。如果 myDataGridView.Rows.Count == 0那么您的 DataGridView 是空的。

回答by Brandon

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. It might save you showing a messagebox.

尽管您可能还想查看 DataGridView 的 EmptyDataText 属性。它可能会节省您显示消息框的时间。

回答by Matthew Groves

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.

根据 Linq 结果,您可以隐藏 datagridview 并显示一些其他显示消息的控件(如文字或其他东西)。如果您想要某种消息框弹出窗口,则需要在其中添加一些 JavaScript。

回答by Apoorv Khurasia

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.

这里的很多答案都有参考Rows.Count。通常,这不会造成问题,而且在大多数情况下,执行我将要提出的建议是一种矫枉过正。

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

但是由于本文档中提到的原因,Rows.Count如果DataGridView经常有大量数据(>~ 5000内存分析中的单元格我在一段时间内验证该文章时所做的),调用它可能不是一个好主意。

Avoid using the Countproperty of the System.Windows.Forms.DataGridViewSelectedCellCollectionto determine the number of selected cells. Instead, use the DataGridView.GetCellCountmethod and pass in the DataGridViewElementStates.Selectedvalue. Similarly, use the DataGridViewRowCollection.GetRowCountand DataGridViewColumnCollection.GetColumnCountmethods to determine the number of selected elements, rather than accessing the selected row and column collections.

避免使用 的Count属性 System.Windows.Forms.DataGridViewSelectedCellCollection来确定所选单元格的数量。相反,使用该 DataGridView.GetCellCount方法并传入 DataGridViewElementStates.Selected值。同样,使用 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 ——它不会造成太大伤害。

回答by Fatos SYLAJ

This should do it:

这应该这样做:

dataGridView1.RowCount == 0

回答by Ashraf Abusada

The DGV.Rows.Countmethod of checking if DGV is empty does not work if the option AllowUserToAddRowsis set to true.

DGV.Rows.Count如果选择检查的方法,如果DGV是空不起作用AllowUserToAddRows设置为true。

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

您必须禁用AllowUserToAddRows = false然后像这样检查是否为空:

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

回答by Bijay Budhathoki

//this gives rows count=1

//这给出了行数=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)