views:

902

answers:

3

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

A: 

You're looking for the CellFormatting event.
Here is an example.

SLaks
+1  A: 

Something like the following... assuming the values in the cells are Integers.

foreach (DataGridViewRow dgvr in myDGV.Rows)
{
  if (dgvr.Cells[7].Value < dgvr.Cells[10].Value)
  {
    dgvr.DefaultCellStyle.ForeColor = Color.Red;
  }
}

untested, so apologies for any error.

If you know the particular row, you can skip the iteration:

if (myDGV.Rows[theRowIndex].Cells[7].Value < myDGV.Rows[theRowIndex].Cells[10].Value)
{
  dgvr.DefaultCellStyle.ForeColor = Color.Red;
}
Demi
Thank you for your help. Your suggestion is the closest I've gotten to solving the problem. But I keep getting the error saying either "Value" doesn't exist in context or "Cells" doesn't exist in context. Trying to figure it out...
EB
this line of code(dgvr.Cells[7].Value < dgvr.Cells[10].Value)now gives me this errorOperator '<' cannot be applied to operands of type 'object' and 'object'
EB
Cast them to Integer, then. :-)something like:Convert.ToInt32(dvgr.Cells[7].Value) < Convert.ToInt32(dgvr.Cells[10].Value)
Demi
A: 

I typically Like to use the GridView.RowDataBound Event event for this.

protected void OrdersGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.ForeColor = System.Drawing.Color.Red;
        }
    }
Edison