开发者

How can I change column fore color in DataGridview as per condition?

I have one table like employee and one of its row is 'status'. If status value is 'approve' then I wa开发者_JS百科nt to show that row in a green color otherwise I want to show it in a red color. I have tried following but it is not working

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

ALSO THIS ONE

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

But in this case i'm getting error for datagridviewcellformattingeventargs I'm using VS2010


hi all i got the solution plz c this one ;)

just write the following condition code on 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;
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜