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
单元格具有 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