日期:2014-05-18  浏览次数:20445 次

在GridView中如何让符合条件的数据字体变色-在线结贴(100分)
在GridView中如何让符合条件的数据字体变色;有一点难度。 gridview 列A1(int),A2,A3,A4,A5,A6 后面的是是varchar
在gridview里显示如下;
  a1 a2(名称) a3(检查结果) a4(数值型下限) a5(数值型上限) a6(字符型标准)  
  1 x1 营养不良 良好
  1 x2 肥胖 正常
  1 x3 10 8 20  
  1 x4 50 30 40
  2 x1 良好 良好
  2 x2 正常 正常
  2 c1 80 70 100
  2 c2 30 10 20
  2 c3 45 20 40
............(略)
想要得到结果在gridview里  
a3列的内容是 数值型的数据 用 a4 和a5 判断 如果不在两者之间就显示红色 例;x4的50 不在 30-40之间 就显示红色
c2和c3 也同样;(我用了好多方法都不能达到要求,各位同仁有没有更好的方法)能回答上面的也给满分!!!, 能顺便把下面的回答了更好。

a3(检查结果) 是汉字的 比对 a6(字符型标准) 如果不同就显示红色 如; 营养不良--和良好不匹配 显示红色; 肥胖--正常不匹配 显示红色
用sql语句也是可以的但是太麻烦了。长期在线 测试过马上给分!!


------解决方案--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string result = e.Row.Cells[2].Text;
int temp;
if (int.TryParse(result,out temp))
{
if (Convert.ToInt32(e.Row.Cells[2].Text) < Convert.ToInt32(e.Row.Cells[3].Text)
|| Convert.ToInt32(e.Row.Cells[2].Text) > Convert.ToInt32(e.Row.Cells[4].Text))
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
else
{
if (e.Row.Cells[2].Text != e.Row.Cells[4].Text)
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
}
}
------解决方案--------------------
你的问题解了吗? 按照你的描述 我认为这样是最佳的

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) 
{
if (e.Row.Cells[3].Text != "&nbsp;" || e.Row.Cells[4].Text != "&nbsp;" || e.Row.Cells[5].Text != "&nbsp;")
{
string result = e.Row.Cells[2].Text.Trim (); 
double temp; 
if (Double.TryParse(result,out temp)) 

if (Convert.ToDouble(e.Row.Cells[2].Text.Trim ()) < Convert.ToDouble(e.Row.Cells[3].Text.Trim ()) 
||Convert.ToDouble(e.Row.Cells[2].Text.Trim ()) > Convert.ToDouble(e.Row.Cells[4].Text.Trim ())) 

e.Row.Cells[2].BackColor = System.Drawing.Color.Red; 


else 

if (e.Row.Cells[2].Text.Trim () != e.Row.Cells[5].Text.Trim ()) 

e.Row.Cells[2].BackColor = System.Drawing.Color.Red; 


}

}你最后一个判断的是第6行。 
重要的是你上面提到的 &nbsp 是非间断空值 不能完全理解为空格,也不是能判断为空。否则报错;