Datagridview Rows Change Color

Im creating an inventory system that checks the lines in cell 5 every 3 seconds if it is less than 10. Then my problem is how to change the color to red so that they are lower at 10.

private void belowstock()
    {
        int row;
        int qty, qtyOnHand;

        for (row = 0; row < dataGridView1.RowCount; row++)
        {
            qty = int.Parse(dataGridView1.Rows[row].Cells[5].Value.ToString());

            qtyOnHand = 10;
            if (qty <= qtyOnHand)
            {

                   //red  
             }     

            else
                  //white
       }
    }
+3
source share
2 answers

LINQ

way to do it

private void belowstock()
{
    dataGridView1.Rows.Cast<DataGridViewRow>().Where(w => (int)w.Cells[5].Value < 10).ToList().ForEach(f => f.DefaultCellStyle.BackColor = Color.Red);
    dataGridView1.Rows.Cast<DataGridViewRow>().Where(w => (int)w.Cells[5].Value > 10).ToList().ForEach(f => f.DefaultCellStyle.BackColor = Color.White);
}

Just enter this code

+3
source

try the code below in your loop:

row.DefaultCellStyle.BackColor = Color.Red;
+3
source

All Articles