【问题标题】:Check if dataGridView is empty检查dataGridView是否为空
【发布时间】:2011-10-15 15:58:39
【问题描述】:

我需要一种简单的方法来检查我的 datagridview 是否为空。
Rows.Count 不满足我,因为我的程序以 2 个空行开头,
并且将来可以填充 datagridview 然后计数
不影响任何东西
(如果用户删除了一个部分但存在超过 2 行)。

有没有办法检查这个?

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    这些是检查数据网格视图是否为空的选项......

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

    2)。您可以检查绑定到的 DataTable 或 DataSet 数据网格视图:

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

    您也可以使用以下方法检查数据网格视图单元格值:

    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;
            }
        }
    

    【讨论】:

      【解决方案2】:

      dataGridView1 启用添加:

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

      dataGridView1 禁用添加:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-22
        • 2021-08-17
        • 1970-01-01
        相关资源
        最近更新 更多