IF语句中有错误。我想使用> =条件,但这个条件不被接受,但==接受我的程序。请帮我。 [英] There are error in IF statement. I want to use >= condition but this condition not accepted but == is accepted my program. Please help me.
本文介绍了IF语句中有错误。我想使用> =条件,但这个条件不被接受,但==接受我的程序。请帮我。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
请看下面的代码。
条件有问题。请建议我能做些什么?
我尝试过:
for ( int i = 0 ; i < dataGridView1.Rows.Count; i ++)
{
if (dataGridView1.Rows [i] .Cells [ 1 ]。Value.ToString()== 30)
{
dataGridView1.Rows [i] .Cells [ 1 ]。Style.BackColor = Color.Red;
dataGridView1.Rows [i] .Cells [ 0 ]。Style.BackColor = Color.Red;
}
else
{
dataGridView1.Rows [i] .Cells [ 1 ]。Style.BackColor = Color.Green;
dataGridView1.Rows [i] .Cells [ 0 ]。Style.BackColor = Color.Green;
}
}
}
解决方案
引用:if(dataGridView1.Rows [i] .Cells [1] .Value.ToString()==30)
绝不使用字符串表示法比较数值。
获取dataGridView1.Rows [i] .Cells [1] .Value
的数值(您可以按照建议使用int.TryParse
方法)然后将它与int
值进行比较30
Hello All,
Please see below code.
there are something wrong in if condition. kindly suggest me what i can do?
What I have tried:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value.ToString() == "30")
{
dataGridView1.Rows[i].Cells[1].Style.BackColor = Color.Red;
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Red;
}
else
{
dataGridView1.Rows[i].Cells[1].Style.BackColor = Color.Green;
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Green;
}
}
}
解决方案
Quote:if (dataGridView1.Rows[i].Cells[1].Value.ToString() == "30")
Never compare numeric values using their string representations.
Get the numeric value ofdataGridView1.Rows[i].Cells[1].Value
(you may use, as suggested, theint.TryParse
method) and then compare it with theint
value30
.
这篇关于IF语句中有错误。我想使用> =条件,但这个条件不被接受,但==接受我的程序。请帮我。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文