c# - 如何根据条件更改 DataGridview 中的列前景色?

标签 c# gridview

我有一张像员工这样的表,其中一行是“状态”。 如果状态值为“批准”,那么我想以绿色显示该行 否则我想用红色显示它。 我尝试过以下操作,但不起作用

if (e.Row.RowType == DataControlRowType.Header)
{ 
    string status = DataBinder.Eval(e.Row.DataItem, "IsApprove").ToString();
    if (status == "pending")
    {
        e.Row.ForeColor = System.Drawing.Color.Red; // Change the row's Text color
    }
}

还有这个

private void gvleavedetail_cellformatting(object sender, datagridviewcellformattingeventargs e)
{
    // if the column is the artist column, check the
    // value.
    if (this.gvleavedetail.columns[e.columnindex].name == "artist")
    {
        if (e.value != null)
        {
            // check for the string "pink" in the cell.
            string stringvalue = (string)e.value;
            stringvalue = stringvalue.tolower();
            if (stringvalue == "high")
            {
                e.cellstyle.backcolor = color.pink;
            }
        }
    }

但在这种情况下,我收到 datagridviewcellformattingeventargs 错误 我使用的是VS2010

最佳答案

大家好,我得到了解决方案,请c这个;)

只需在 RowDataBound EVENT OF GRIDVIEW 上编写以下条件代码

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "IsApprove"));

            if (status == "pending")
            {

                e.Row.Cells[7].ForeColor = System.Drawing.Color.Yellow;
            }
            else if(status== "accept")
            {

                e.Row.Cells[7].ForeColor = System.Drawing.Color.Green;
            }
            else if (status == "reject")
            {

                e.Row.Cells[7].ForeColor = System.Drawing.Color.Red;
            }
        }
    }

关于c# - 如何根据条件更改 DataGridview 中的列前景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4546487/

相关文章:

c# - Linq Lookup 解析 CSV 文本行

javascript - 如何使用 Controller 中的对象创建 js

c# - 使用自定义模板字段进行 Gridview 排序

c# - 谁能给我解释一下这个 lambda 表达式?这让我发疯

c# - C# 中小方法调用的成本和优化

c# - 我们可以用单个 WCF 调用替换多个 WCF 调用吗?

android - GridView - fragment - LongClick

android - 如何使用 viewpager 将 GridView 居中放置在 fragment 中

javascript - 无法在加载时加载 Gridview

java - Gridview 的 URI 列表