datagridview中的空单元格 [英] empty cell in datagridview

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

问题描述

你好



我在访问datagridview中的单元格时得到nullReferenceException。

我试过这些来检查空单元格(所以不要访问它),它们都不适合我。任何帮助??

Hello

I am getting a nullReferenceException when accessing cells in datagridview.
I've tried these to check for the empty cell (so to not access it), none of them is working for me. Any help??

if (dataGridView2.Rows[0].Cells[col].Value!=DBNull.Value)

if (string.IsNullOrEmpty(dataGridView2.Rows[0].Cells[col].Value.ToString()) == false)

String s=dataGridView2.Rows[0].Cells[col].Value.ToString();
if(IsNullOrEmpty(s))

推荐答案

如何关于这个:

How about this:
if (dataGridView2.Columns[e.ColumnIndex].Name == "ColumnName")
        {
            if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
            {
                dataGridView1.Rows[e.RowIndex].ErrorText =
                    "Column Name must not be empty";
                e.Cancel = true;
            }
        }



也可以使用:


Also can use:

foreach (DataGridViewRow rw in this.dataGridView2.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())
    {
      // Do your stuff...
    }
  }
}


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

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