C#检查datagridview列是否为空 [英] C# check if datagridview column is empty

查看:404
本文介绍了C#检查datagridview列是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查datagridview中的很多"列是否为空.

这是我的代码

I want to check if my "many" column in datagridview is empty.

here my code

for (int i = 0; i < (gridx.Rows.Count - 1); i++)
        {
            col = gridx.Rows[i].Cells["code"].Value.ToString();
            col4 = gridx.Rows[i].Cells["many"].Value.ToString();                
        }
if (col4 == "")
        {
            MessageBox.Show("Many is empty");
            this.Focus();
            return;
        } 
else
{
//my code in here
}


但它不显示错误很多为空"

请帮助我..并且在


but it don''t show error "many is empty"

please help me.. and thanks before

推荐答案

之前感谢,如果不进行调试,您将永远无法确定要获取的数据.至于与空字符串的比较,我将使用 String.IsNullOrWhiteSpace [^ ]方法.它只是检查一个空字符串来检查更多条件.
Well, without debugging you can never be sure what data you are getting. As far as comparision with empty string is concerned, I would use String.IsNullOrWhiteSpace[^] Method. It checks more conditions tha just an empty string.


看看下面的代码是否有帮助:

See if below code helps:

bool flag = false;
foreach(DataGridViewRow row in gridx)
{
  if(Convert.ToString(row.Cells["many"].Value) == string.Empty)
     flag = true;
}

if(flag)
  MessageBox.Show("Many is empty");
else
 // your code  


单元格具有<一个href ="http://msdn.microsoft.com/de-de/library/system.windows.forms.datagridviewcell.formattedvalue.aspx"> FormattedValue 属性,该属性获取包含为单元格的对象的格式化表示形式" s的值,即,如果cell.Value为null,则将看到空字符串,依此类推.
Cell has FormattedValue property which gets formatted representation of object contained as cell''s value, i.e. if cell.Value is null you''ll see empty string and so on.
foreach(DataGridViewRow row in yourDatagridView.Rows)
{   
    if(string.IsNullOrWhiteSpace(row.Cells["many"].FormattedValue))
    {
        MessageBox("BOOM! Many is empty",":(");
        return;
    }
}

//rest of your code here


这篇关于C#检查datagridview列是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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