[英]how to check all column cell of datagridview? if one or more cell is empty, it will return a value?
我想检查我的datagridview的单元格。 另外,如果至少一个单元格为空,则将返回“ false”
所以,我有这种方法,我知道这是不正确的。 因为如果数组中的最后一个项目都具有值,那么它将使“ checking”成为true。 我只是无法获得我计划制作的正确代码或逻辑。
public void checker()
{
string[] check = new string[50];
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
check[i] = dataGridView1.Rows[i].Cells[0].Value.ToString();
}
for (int e = 0; e < check.Length; e++)
{
if (check[e] == null)
{
checking = "false";
}
else
{
checking = "true";
}
}
}
您可以在第一次发现空单元格时从函数中返回。 您可以尝试使用以下方法:
public void checker()
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
var value = dataGridView1.Rows[i].Cells[0].Value.ToString();
if (string.IsNullOrWhiteSpace(value))
{
checking = false;
return;
}
}
// If we have reached this far, then none of the cells were empty.
checking = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.