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
C# DataGridView Check if empty
提问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 == 0
then 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.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).
但是由于本文档中提到的原因,Rows.Count
如果DataGridView
经常有大量数据(>~ 5000
内存分析中的单元格我在一段时间内验证该文章时所做的),调用它可能不是一个好主意。
Avoid using the
Count
property of theSystem.Windows.Forms.DataGridViewSelectedCellCollection
to determine the number of selected cells. Instead, use theDataGridView.GetCellCount
method and pass in theDataGridViewElementStates.Selected
value. Similarly, use theDataGridViewRowCollection.GetRowCount
andDataGridViewColumnCollection.GetColumnCount
methods 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.GetRowCount
和DataGridViewColumnCollection.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.Count
method of checking if DGV is empty does not work if the option AllowUserToAddRows
is set to true.
该DGV.Rows.Count
如果选择检查的方法,如果DGV是空不起作用AllowUserToAddRows
设置为true。
You have to disable AllowUserToAddRows = false
then 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)