如何隐藏特定的行C# [英] How do I hide a specific row C#
问题描述
我想隐藏一行,如果它有一个单元格包含'花店',如果标签上写着'BMX商店'
I want to hide a row if it has a cell contains 'flower shop', if the label says 'BMX shop'
if (label3.Text == "BMX shop")
foreach (DataGridViewRow dr in CustOrdersDGV.Rows)
{
if (dr.Cells[7].Value.ToString().ToLower().Contains("Flower shop"))
{
dr.Visible = false;
}
}
但是当代码运行时,它显示所有行
我的尝试:
But when the code is run, it shows all rows
What I have tried:
if (dr.Cells[7].Value.ToString() == "BMX shop")
{
dr.Visible = false;
}
和......
and ...
foreach (DataGridViewRow dr in CustOrdersDGV.Rows)
{
if (dr.Cells.Count > 7) // validate the index
if (dr.Rows[7].Value.ToString() == "BMX shop")
{
dr.Visible = false;
}
}
推荐答案
看看你的代码:
Look at your code:
if (dr.Cells[7].Value.ToString().ToLower().Contains("Flower shop"))
由于ToLower总是删除任何大写字符,你永远不会得到任何包含大写字母的文本匹配...
你的其他示例代码使用不同的列号,所以其中一个是可能是错的。 (这就是为什么使用列名更好的原因 - 不容易出错。)
除此之外,你需要使用调试器来查看代码运行时的列数据 - 我们无法为您执行此操作,因为我们无法访问您的数据!
Since ToLower always removes any uppercase characters, you will never get a match with any text containing uppercase letters...
And your other example code uses a different column number, so one of them is probably wrong. (This is why it's a good idea to use the column name instead - less prone to error.)
Other than that, you need to use the debugger to look at your column data while your code is running - and we can't do that for you as we have no access to your data!
这篇关于如何隐藏特定的行C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!