在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语句也是可以的但是太麻烦了。长期在线 测试过马上给分!!

解决方案 »

  1.   

    新版的编辑样式和显示格式竟然不一样    格式是入下表
     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 
      

  2.   

    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;
                    }
                }
            }
        }
      

  3.   

    你可以试一下gridthemes,里面有很多格式可用
    http://www.codeproject.com/aspnet/GridThemes.asp
      

  4.   

    用后期邦定,方法类似2楼的。用模版列 
    protected string BoundInfo(string str1,string str2,string str3,string str4)
    {
              if (Convert.ToInt32(str1)< Convert.ToInt32(str2) 
                         ¦ ¦ Convert.ToInt32(str3)  > Convert.ToInt32(str4)) 
                    { 
                        return <font color=red>str1</font>; 
                    } }
      

  5.   

    <asp:Label BackColor='<%# (Convert.ToInt32(Eval("a3"))< Convert.ToInt32(Eval("a4"))) || (Convert.ToInt32(Eval("a3"))> Convert.ToInt32(Eval("a5")))? System.Drawing.Color.Red : System.Drawing.Color.Black %>' ..../>
      

  6.   

     if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[8].Text == "USA")
                {
                    //e.Row.BackColor = System.Drawing.Color.Red;
                    e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
                }
            }类似的你改一下就行了
      

  7.   

    我继续测试2楼兄弟的给的代码;不太明白的地方是  当我的数据库 
    第三列(e.Row.Cells[2].Text)和第四列(e.Row.Cells[4].Text)为空的时候,
    这里的三、四列的值是 &nbsp  然后就报错类型错误。  可是空的 我又不能跳过去,我再测试
      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)
      

  8.   

    呵呵 谢谢你, 我争取今天晚上搞定,这个新版bbs录入框的编辑样式和发布上来的样式不一样 我接着努力
      

  9.   

    还是问2楼现在的数据  三列和四列 有小数点的情况下就没有办法判断了
    例如  三列是 5.8  三列是3,四列是10这个也是红色因为是text类型的吧
    我这里有小数点的列还挺多,大部分正常值的范围内也变成红色。这个问题你能给我个建议吗? 
      

  10.   

    还是问2楼现在的数据  e.Row.Cells[2].Text 有小数点的情况下就没有办法判断了 
    例如  三列(三列就a3也就是e.Row.Cells[2].Text)是 5.8  四列是3,5列是10这个也是红色因为是text类型的吧 
    这里的第四列(e.Row.Cells[3].Text)和第5列(e.Row.Cells[4].Text)
    我这里有小数点的列还挺多,大部分正常值的范围内也变成红色。这个问题你能给我个建议吗? 
      

  11.   

    将类型转换成double就可以了
    把转换的那段改成下面就可以了,其实如果你给的数据有小数点的话
    我早就写成这样了:)
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string result = e.Row.Cells[2].Text;
                double temp;
                if (Double.TryParse(result,out temp))
                {
                    if (Convert.ToDouble(e.Row.Cells[2].Text) < Convert.ToDouble(e.Row.Cells[3].Text)
                        || Convert.ToDouble(e.Row.Cells[2].Text) > Convert.ToDouble(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;
                    }
                }
            }
        }            
      

  12.   

    hehe 谢谢 刚才再最后问你之前 我测试过用 double型  可是还是有数据在范围之内  变红色(和第三个判断值无关)  我再继续找原因, 我也决得挺奇怪的。
      

  13.   

     public void Bind()
        {
            string conn = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("./data/english.mdb");
            string cmd = "select id,title,type,dt from News";
            OleDbDataAdapter da = new OleDbDataAdapter(cmd, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "News");
            GridView1.DataSource = ds;
            //设置主键;
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.DataBind();
            //解决方案:主要是绑定后过滤
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                DataRowView mydrv = ds.Tables["News"].DefaultView[i];
                string Temp = mydrv["type"].ToString();
                if (Temp == "娱乐新闻")   //大家这里根据具体情况设置可能ToInt32等等
                {
                    GridView1.Rows[i].Cells[2].BackColor = System.Drawing.Color.Red; //这里要修改单元格位置;
                }
            }     }    protected void Page_Load(object sender, EventArgs e)
        {
            Bind();
        }
      

  14.   

    hehe  图片传不上来  
      

  15.   

    如果是这样 那你把每个取值地方都加上Trim
    如e.Row.Cells[2].Text.Trim()
    以免不规则数据的干扰
      

  16.   

    刚才就加上了  而且是 
    if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
                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; 
                    } 
                } 
            } 
    我再换换float测试一下,我们是不是该想的地方都想到了,问题就有点奇怪了  呵呵
      

  17.   

    你的问题解了吗? 按照你的描述 我认为这样是最佳的  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 是非间断空值 不能完全理解为空格,也不是能判断为空。否则报错;