我想检查datagridview中的"many"列是否为空.
我的代码
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
}
Run Code Online (Sandbox Code Playgroud)
但它没有显示错误"很多是空的"
请帮帮我..谢谢之前
由于您要col4在for循环中指定最后一个单元格值,因此可以将其作为null进行检查.
for (int i = 0; i < (gridx.Rows.Count - 1); i++)
{
col = gridx.Rows[i].Cells["code"].Value.ToString();
if(gridxRows[i].Cells["many"].Value == null ||
gridxRows[i].Cells["many"].Value == string.Empty)
{
col4 = gridx.Rows[i].Cells["many"].Value.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
您当前的代码只会检查最后一行cell["many"]是否为空.如果您想确保所有列都是空的,那么您可以尝试以下方法.
bool isColumnEmpty = true;
for (int i = 0; i < (gridx.Rows.Count - 1); i++)
{
col = gridx.Rows[i].Cells["code"].Value.ToString();
if(gridxRows[i].Cells["many"].Value == null ||
gridxRows[i].Cells["many"].Value == string.Empty)
{
col4 = gridx.Rows[i].Cells["many"].Value.ToString();
isColumnEmpty = false; // that means some thing is found against cell
}
}
Run Code Online (Sandbox Code Playgroud)
现在检查是否设置了isColumnEmpty标志;
if(isCoulmnEmpty)
{
MessageBox.Show("Many is empty");
this.Focus();
return;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14919 次 |
| 最近记录: |