如何使用C#检查datagridview中的空和null单元格 [英] How to check empty and null cells in datagridview using C#

查看:1217
本文介绍了如何使用C#检查datagridview中的空和null单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查datagridview单元格的空值和空值...但我不能这样做...

i am trying to check the datagridview cells for empty and null value... but i can not do it right...

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if ((String)dataGridView1.Rows[i].Cells[3].Value == String.Empty)
    {
        MessageBox.Show(" cell is empty");
        return;
    }

    if ((String)dataGridView1.Rows[i].Cells[3].Value == "")
    {
        MessageBox.Show("cell in empty");
        return ;
    }
}

我甚至试过这个代码

if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value))
{
  MessageBox.Show("cell is empty");
  return;
}

可以帮助我..这个...

can any one help me.. with this...

推荐答案

我会尝试这样:

foreach (DataGridViewRow rw in this.dataGridView1.Rows)
{
  for (int i = 0; i < rw.Cells.Count; i++)
  {
    if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString())
    {
      // here is your message box...
    }
  } 
}

这篇关于如何使用C#检查datagridview中的空和null单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆