CHANGE FORECOLOR AF A SPECIAL WORD IN GRIDVIEW CELL

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 928  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. Pravesh Singh

    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;

            }

        }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!