C# 如何根据条件语句更改gridview单元格的背景色?

C# 如何根据条件语句更改gridview单元格的背景色?,c#,css,gridview,code-behind,C#,Css,Gridview,Code Behind,好吧,我显然没有给谷歌提供正确的查询,否则我现在就知道了。我希望这个论坛上有人能帮助我 所以我有一个datatable,我在datareader的基础上添加行,datareader从数据库上执行的sql查询中获取信息。到目前为止,一切顺利。现在,其中一列称为“分析”,如果前面两列匹配,我需要它的背景颜色为绿色,否则为红色 如果我不能触摸背景色,我想插入一个特殊字符,任何不解释为文本的javascript 简单地说,我希望css从codebehind控制gridview。我看了又看都没用。我找到了

好吧,我显然没有给谷歌提供正确的查询,否则我现在就知道了。我希望这个论坛上有人能帮助我

所以我有一个datatable,我在datareader的基础上添加行,datareader从数据库上执行的sql查询中获取信息。到目前为止,一切顺利。现在,其中一列称为“分析”,如果前面两列匹配,我需要它的背景颜色为绿色,否则为红色

如果我不能触摸背景色,我想插入一个特殊字符,任何不解释为文本的javascript


简单地说,我希望css从codebehind控制gridview。我看了又看都没用。我找到了盖伊,但我还没有检查他的解决方案在ASP.Net/C网站上是否有效。有什么想法吗?

GridView\u RowDataBound事件中,获取要更改背景颜色的单元格,如果您的条件为真,则设置单元格的颜色

/// <summary>
/// Handles gridview row data bound event.
/// </summary>
/// <param name="sender">Sender Object</param>
/// <param name="e">Event Argument</param>
protected void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // We don’t want to apply this to headers.
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        try
        {
            //your data-object that is rendered in this row, if at all required.
            //Object obj = e.Row.DataItem;

            //find the right color from condition
            string color = condition ? "#ff9900" : "some-other-color";

            //set the backcolor of the cell based on the condition
            e.Row.Cells[4].Attributes.Add("Style", "background-color: " + color + ";");
        }
        catch
        {
        }
    }
}
//
///处理gridview行数据绑定事件。
/// 
///发送方对象
///事件参数
受保护的void Gv_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
//我们不想将此应用于标题。
如果(e.Row.RowType==DataControlRowType.DataRow)
{
尝试
{
//在此行中呈现的数据对象(如果需要)。
//Object obj=e.Row.DataItem;
//从条件中找到正确的颜色
字符串颜色=条件?#ff9900:“其他颜色”;
//根据条件设置单元格的背景色
e、 行。单元格[4]。属性。添加(“样式”,“背景色:“+color+”;”);
}
抓住
{
}
}
}

虽然此代码块可能会回答这个问题,但最好能对其原因进行一点解释
 protected void GVKeywordReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow pr = ((DataRowView)e.Row.DataItem).Row;
                int oldPos = Convert.ToInt32(pr["oldposition"]);
                int newPos = Convert.ToInt32(pr["newposition"]);
                GVKeywordReport.HeaderRow.Cells[3].Text = txtfrmdate.Text;
                GVKeywordReport.HeaderRow.Cells[4].Text = txtEndDate.Text;

                GVKeywordReport.HeaderRow.BackColor = ColorTranslator.FromHtml("#B3B300");
                e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#B3B300");
                e.Row.Cells[5].BackColor = ColorTranslator.FromHtml("#FFFFFF");

                if (oldPos == newPos)
                {
                    e.Row.BackColor = ColorTranslator.FromHtml("#FF950E");
                    e.Row.Cells[6].Text = "No Change";
                }
                else if (oldPos > newPos)
                {
                    e.Row.BackColor = ColorTranslator.FromHtml("#FFFFCC");
                    e.Row.Cells[6].Text = "Improved";
                }
                else  

                {
                    e.Row.BackColor = ColorTranslator.FromHtml("#FF0000");
                    e.Row.Cells[6].Text = "Decreased";
                }
               // e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#7DA647");
            }
        }