change forecolor af a special word in gridview cell

Total Post:183

Points:1285
C# 
C#
 1169  View(s)
Ratings:
Rate this:

I want to change a color of a special word NOT all words of a gridview Cell . here is event and codes:

protected void gvContents_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[3].Text.Contains("Special"))
        {
            //set The "Special" word only forecolor to red
        }
        else if (e.Row.Cells[3].Text == "Perishable")
        {
            //set The "Perishable" word only forecolor to blue
        }
        else if (e.Row.Cells[3].Text == "Danger")
        {
            //set The "Danger" word only forecolor to yellow
        }
    }
}

and Cell text might be like here :"Radioactive : Danger" or this : "Human Body : Special ,Perishable " what should I do?

  1. Post:412

    Points:2888
    Re: change forecolor af a special word in gridview cell

    Hi ankita,


    In the CellFormatting event handler, add the below code

    void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value != null && e.Value.ToString() == "Special")
            {
                e.CellStyle.ForeColor = Color.Red;
            }
        }

      Modified On Apr-04-2018 01:40:01 AM

Answer