简体   繁体   English

检查dataGridView是否为空

[英]Check if dataGridView is empty

I am in need of a simple way for checking if my datagridview is/isn't empty. 我需要一种简单的方法来检查我的datagridview是否为空。
The Rows.Count does not satisfy me , because my program starts with 2 empty rows, Rows.Count不满足我的要求 ,因为我的程序以2个空行开头,
and in the future the datagridview could be populated and then the count 将来可能会填充datagridview,然后计数
does not affect anything 不影响任何事情
(If the users deletes a section but there are more than 2 rows present). (如果用户删除了一个节,但是存在多于2行)。

Is there anyway of checking this? 反正有检查吗?

well these are the checking options for whether datagrid view is empty or not ...... 好吧,这些是datagrid视图是否为空的检查选项……

if(DataGridView1.Rows.Count == 0)
{
    MessageBox.Show("DataGridView is empty");
}

2). 2)。 You can check the DataTable or DataSet which binds to DataGridView: 您可以检查绑定到DataGridView的DataTable或DataSet:

if(dt == null)
{
   MessageBox.Show("DataGridView is empty");
}

if(ds == null)
{
   MessageBox.Show("DataGridView is empty");
}

you can check with datagrid view cell value also by using this: 您也可以使用以下方法检查datagrid视图单元格的值:

if (dataGridView1.Columns[e.ColumnIndex].Name == "companyName")
    {
        if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
        {
            dataGridView1.Rows[e.RowIndex].ErrorText =
                "company Name must not be empty";
            e.Cancel = true;
        }
    }

dataGridView1 with enable Adding: 具有启用功能的dataGridView1添加:

using System.Linq;
if (dataGridView1.Rows.OfType<DataGridViewRow>().Take(2).Count() > 1)
        {
            MessageBox.Show("dataGridView1 has at least 2 rows");
        }

dataGridView1 with disable Adding: 具有禁用功能的dataGridView1添加:

if (dataGridView1.Rows.OfType<DataGridViewRow>().Any())
        {
            MessageBox.Show("dataGridView1 has row");
        }

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

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