C# Foreach 循环 - 继续问题
我的 C# Foreach 循环中的 continue 语句有问题。
我希望它检查 datagridview 中是否有空白单元格,如果有,则跳过打印该值并继续检查下一个单元格。
非常感谢帮助。
这是代码:
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Size.IsEmpty)
{
continue;
}
MessageBox.Show(cell.Value.ToString());
}
}
I have a problem with a continue statement in my C# Foreach loop.
I want it to check if there is a blank cell in the datagridview, and if so, then skip printing the value out and carry on to check the next cell.
Help appreciated greatly.
Here is the code:
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Size.IsEmpty)
{
continue;
}
MessageBox.Show(cell.Value.ToString());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,您当前正在检查单元格的大小是否为零。 在网格中,列中的每个单元格具有相同的宽度,行中的每个单元格具有相同的高度(通常,无论如何)。
您希望根据单元格的值进行检查。 例如:
针对您感兴趣的任何其他“空”值表示进行调整。如果有很多,您可能需要为此编写一个特定方法,并在检查中调用它:
Well, you're currently checking whether the cell's size is zero. In a grid, every cell in a column has the same width and every cell in a row has the same height (typically, anyway).
You want to be checking based on the value of the cell. For example:
Tweak this for any other representations of "empty" values that you're interested in. If there are lots, you might want to write a specific method for this, and call it in the check:
您不需要在这里使用 continue 关键字,您可以这样做:
此外,您还检查单元格的大小是否为空。 这真的是你想做的吗?
您遇到什么错误?
You don't need to use the continue keyword here, you could just do this:
Also, you're checking whether the size of the cell is empty. Is this really what you want to do?
What errors are you getting?
您不应该检查单元格的值是否为空而不是大小吗?
Shouldn't you be checking if the cell's value is empty not the size?
我只想读取单元格[1]数据...olny
i want to read only cell[1] data...olny