views:

294

answers:

1

I would like to change the color of a particular row in my datagridview. The row should be changed to red when the value of columncell 7 is less than the value in columncell 10. Any suggestions on how to accomplish this?

Thanks.

EB

+1  A: 

You need to loop through the rows in the datagridview and then compare values of columns 7 and 10 on each row.

foreach (GridViewRow gvrow in GridView1.Rows)
{
//Add comparison code here
{
//Change row's color here
}
}

Based in the code you provided, try this:

foreach (GridViewRow row in vendorsDataGridView.Rows) 
        if (Convert.ToInt32(row.Cells[7].Text) < Convert.ToInt32(row.Cells[10].Text)) 
        {
            row.BackColor = Color.Red; 
        }
Ricardo
Thank you for the help Ricardo. I have tried the code you suggested. I still can't get it to work. Would you mind taking a look at this code and tell me where I've gone wrong? I am a beginning C# student. I'm sure I just haven't written the comparison code correctly. foreach (DataGridView row in vendorsDataGridView.Rows) { if (row.Cells[7].Value is < row.Cells[10].Value) { dataGridViewTextBoxColumn7.DefaultCellStyle.BackColor = red; } }I appreciate your help.EB
EB
EB I added new code based in the code you provided. Your sintax was off a little bit, try the code I just added above.
Ricardo
Ricardo. I changed .text to .value and changed to DefaultCellstyle.Backcolor = color.red and the code worked!!!Thank you for your time!EB
EB