代码之家  ›  专栏  ›  技术社区  ›  Freakishly

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

  •  3
  • Freakishly  · 技术社区  · 14 年前

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

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

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

    简单地说,我希望css从codebhind控制gridview。我看了又看都没用。我发现 this

    2 回复  |  直到 7 年前
        1
  •  3
  •   this. __curious_geek    14 年前

    GridView\行数据绑定 事件,获取要更改背景颜色的单元格,如果条件测试为真,则设置单元格的颜色。

    /// <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
            {
            }
        }
    }
    
        2
  •  0
  •   sanjay    9 年前
     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");
                }
            }