protected void GridViewProfit_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = (e.Row.DataItemIndex + 1).ToString();            if (Convert.ToDecimal(e.Row.Cells[3].Text) > 0)
            {
                e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
            }
            else if (Convert.ToDecimal(e.Row.Cells[3].Text) < 0)
            {
                e.Row.Cells[3].ForeColor = System.Drawing.Color.Green;
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;
            }
        }
    }

解决方案 »

  1.   

    最好用css,这样不仅让程序作了浏览器能干的更好的事,还导致样式不容易扩展。
      

  2.   

    CSS怎么根据表格单元格的值改变颜色啊。
      

  3.   


    if (Convert.ToDecimal(e.Row.Cells[3].Text) > 0)
       {
       e.Row.Cells[3].Text="<font color='#FF0000'>e.Row.Cells[3].Text</font>";
       e.Row.Cells[4].Text="<font color='#FF0000'>e.Row.Cells[4].Text</font>";
       }
     
      

  4.   


    <head runat="server">
        <title></title>
        <style>        
            .simtd
            {
                color: #fff;
                background-color: Blue;
            }
            
            .emptytd
            {
                color: #FFFF00;
                background-color: Green;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" CssClass="table" OnRowDataBound="GridView1_RowDataBound">
        </asp:GridView>
        </form>
    </body>    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.DataItemIndex + 1).ToString();            if (Convert.ToDecimal(e.Row.Cells[0].Text) > 2)
                {
                    //e.Row.CssClass = ""; 定义整行的样式
                    e.Row.Cells[0].CssClass = "simtd";
                }
                else
                {
                    e.Row.Cells[0].CssClass = "emptytd";
                }
            }
        }