【问题标题】:How can I change the background color of a gridview cell, based on a conditional statement?如何根据条件语句更改 gridview 单元格的背景颜色?
【发布时间】:2011-03-09 22:55:33
【问题描述】:

好吧,我显然没有向谷歌提供正确的查询,否则我现在已经发现了。我希望这个论坛上的人可以帮助我。

所以我有一个数据表,我根据数据读取器添加行,该数据读取器从通过数据库执行的 sql 查询获取其信息。到目前为止,一切都很好。现在,其中一列称为“分析”,如果前两列匹配,我需要它的背景颜色为绿色,否则为红色。

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

简单地说,我希望通过代码隐藏对网格视图进行 css 控制。我看了又看都没用。我找到了this 家伙,但我没有检查他的解决方案是否适用于 ASP.Net/C# 网站。有什么想法吗?

【问题讨论】:

    标签: c# css gridview code-behind


    【解决方案1】:
     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");
                }
            }
    

    【讨论】:

    • 虽然这个代码块可能会回答这个问题,但最好能稍微解释一下为什么会这样
    【解决方案2】:

    GridView_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
            {
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 2011-04-03
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多