Bregth Ventanilla
Bregth Ventanilla

Reputation: 21

Change color of datagridview row depending on value C#

I have datagridview which is populated with 6 columns product id,product name,features,price,quantity and total price. All product with 0 quantity is automatically removed from the datagridview. My questions is how do I change color of the cell in "Quantity" if:

  1. If quantity is less than 20 color orange
  2. If quantity is less than 10 color red

Please bear with me as I am new to programming. I am required to do this for the inventory system we made for our capstone project.

Many thanks!

Upvotes: 0

Views: 3356

Answers (1)

Aref Zamani
Aref Zamani

Reputation: 2062

you must first foreach on data grid view rows like following :

 foreach (DataGridViewRow Myrow in dataGridView1.Rows) 

then foreach on cell

foreach(DataGridViewCell cell in Myrow.Cells)

and then convert cells data to int32 and then use DefaultCellStyle.BackColor

    foreach (DataGridViewRow Myrow in dataGridView1.Rows) 
        {       
     foreach(DataGridViewCell cell in Myrow.Cells)
    {       if (Convert.ToInt32(cell.Value)<10)
            Myrow .DefaultCellStyle.BackColor = Color.red;
             else if(Convert.ToInt32(cell.Value)<20) 
Myrow.DefaultCellStyle.BackColor = Color.orange;    
    }


        }

Upvotes: 2

Related Questions