Click here to Skip to main content
14,739,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi friends,

i have a datagridview that i fill it with a table in sql server database. now i want to set a condition that if for example cell[2,3] of datagridview has value of 5 it color must be red;

how can i do this?

thank you.

You can do this on CellFormatting event like below.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    // give the column name which you want to check the value 
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColumnName")
        if (e.Value != null)
            string stringValue = (string)e.Value;
            if (stringValue == "5")
                e.CellStyle.BackColor = Color.Red;

Try dataGridView1.Rows[2].Cells[3].Style.BackColor = Color.Red
_Starbug_ 6-May-14 13:01pm
_Starbug_ 6-May-14 13:02pm
how can i change the color of a rows?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900