Click here to Skip to main content
14,665,095 members
Rate this:
Please Sign up or sign in to vote.
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.
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 2

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

        }
    }
}
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try dataGridView1.Rows[2].Cells[3].Style.BackColor = Color.Red
   
Comments
_Starbug_ 6-May-14 13:01pm
   
thanks
_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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100