I am trying to get a datagrid to display different colour rows based on the quantity of the item.
The problem I’m having is the if statement:
if (RowType == < 5)
is invalid, how do I parse the row type to be checked against a value?
foreach (DataGridViewRow row in dtaPart.Rows)
{
//Cell three is where quantity is
string RowType = row.Cells[3].Value.ToString();
if (RowType == //> 5)
{
row.DefaultCellStyle.BackColor = Color.White;
row.DefaultCellStyle.ForeColor = Color.Black;
}
else if (RowType == //< 5)
{
row.DefaultCellStyle.BackColor = Color.Orange;
row.DefaultCellStyle.ForeColor = Color.Black;
}
else if (RowType == //< 1)
{
row.DefaultCellStyle.BackColor = Color.Red;
row.DefaultCellStyle.ForeColor = Color.Black;
}
}
You can use
double value = double.Parse(RowType)then check ifvalue > 5, < 5, < 1etc.However, if your datagrid column already contains a numeric type, like
doubleorint, you don’t need to callToString()and then parse the string; So instead of this:but you can directly cast the value: