Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据条件更改DataGridview中的列前景色?_C#_Gridview - Fatal编程技术网

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

C# 如何根据条件更改DataGridview中的列前景色?,c#,gridview,C#,Gridview,我有一个类似employee的表,其中一行是“status”。 如果状态值为“批准”,则我希望以绿色显示该行 否则我想用红色显示它。 我试过跟随,但不起作用 if (e.Row.RowType == DataControlRowType.Header) { string status = DataBinder.Eval(e.Row.DataItem, "IsApprove").ToString(); if (status == "pending") {

我有一个类似employee的表,其中一行是“status”。 如果状态值为“批准”,则我希望以绿色显示该行 否则我想用红色显示它。 我试过跟随,但不起作用

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

嗨,我得到的所有解决方案,请看这一个;)

只需在GRIDVIEW的RowDataBound事件上编写以下条件代码

  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;
            }
        }
    }