Rafik Bari
Rafik Bari

Reputation: 5037

Change Cell Style According to its Value?

I'm creating an application that is based on a form and DataGridView control.

I'm binding information from a database and what I'm trying to do now is to change the font style and color of the column propeties according to its value which can be "Urgent","Haute","Normale" .

Here is the code I'm using but it didn't worked, may some one tell me what is going wrong with the code below?

Code:

private void ColorizeCellsbyValue() {

        DataGridViewCellStyle BoldRed = null;
        BoldRed = new DataGridViewCellStyle();
        BoldRed.Font = new Font("Tahoma", 9, FontStyle.Bold);
        BoldRed.ForeColor = Color.Red;

        DataGridViewCellStyle Red = null;
        Red = new DataGridViewCellStyle();
        Red.ForeColor = Color.Red;

        DataGridViewCellStyle Black = null;
        Black = new DataGridViewCellStyle();
        Black.ForeColor = Color.Black;
        string priority;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            priority = row.Cells[3].Value.ToString();
            switch (priority)
            {
                //Change font 
                case "Urgent":
                    row.Cells[3].Style = BoldRed;
                    break;
                case "Haute":
                    row.Cells[3].Style = Red;
                    break;
                case "Normale":
                    row.Cells[3].Style = Black;
                    break;
                default:
                    row.Cells[3].Style = Black;
                    break;
            }
        }
}

Upvotes: 0

Views: 2396

Answers (2)

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9950

Try using

row.Cells[3].Style.BackColor = <Color>;

for ForeColor use

row.Cells[3].Style.ForeColor = <Color>;

This should work. Happy coding!

Upvotes: 1

spajce
spajce

Reputation: 7092

You don't need to create a DataGridViewCellStyle to design your Column Properties. try to figure out my simple example

        foreach (DataGridViewRow rows in dataGridView1.Rows)
        {
            if (rows.Cells[3].RowIndex % 2 == 0)
            {
                rows.Cells[3].Style.Font = new Font("Tahoma", 9, FontStyle.Bold);
                rows.Cells[3].Style.BackColor = Color.Red;
            }
            else
            {
                rows.Cells[3].Style.Font = new Font("Arial", 9, FontStyle.Regular);
                rows.Cells[3].Style.BackColor = Color.Blue;
            }
        }

and my answer to your main problem is try to use .EditedFormattedValue.ToString()

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            priority = row.Cells[3].EditedFormattedValue.ToString();
            switch (priority)
            {
                //Change font 
                case "Urgent":
                    row.Cells[3].Style = BoldRed;
                    break;
                case "Haute":
                    row.Cells[3].Style = Red;
                    break;
                case "Normale":
                    row.Cells[3].Style = Black;
                    break;
                default:
                    row.Cells[3].Style = Black;
                    break;
            }
        }

Upvotes: 1

Related Questions