Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!

What I have tried:

I have no idea where to start but this is the code we used for datagrdiview1:

public void GetData()
 {
     try
     {
         con = new SqlConnection(cs.DBConn);
         con.Open();
         String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity),sum(Price*Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID group by Product.productID,productname,Price,Features,Quantity having(Quantity>0)  order by ProductName";
         cmd = new SqlCommand(sql, con);
         rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
         dataGridView1.Rows.Clear();
         while (rdr.Read() == true)
         {
             dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5]);
         }
         con.Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Posted
Updated 28-Nov-17 20:01pm

try
int columnIndex = 4; // quantity column , zero based index
          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
              int quantity;
              if (int.TryParse(row.Cells[columnIndex].Value.ToString(), out quantity))
              {
                  if (quantity < 20)
                      row.Cells[columnIndex].Style.BackColor = System.Drawing.Color.Red;
                  if (quantity < 10)
                      row.Cells[columnIndex].Style.BackColor = System.Drawing.Color.Orange;
              }
          }
 
Share this answer
 
Comments
Bregth Ventanilla 29-Nov-17 6:22am    
Worked perfectly! Many thanks :)
Karthik_Mahalingam 29-Nov-17 6:27am    
welcome :)
You can use GridView.RowDataBound Event (System.Web.UI.WebControls)[^]

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TableCell cell = e.Row.Cells[4];
        int quantity = int.Parse(cell.Text);
        if (quantity < 20)
        {
            cell.BackColor = Color.Orange;
        }
        if (quantity < 10)
        {
            cell.BackColor = Color.Red;
        }       
    }
}
 
Share this answer
 
Comments
Bregth Ventanilla 29-Nov-17 6:23am    
Worked perfectly! Many thanks :)
RaviRanjanKr 30-Nov-17 9:11am    
glad it helped you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900