0

I am trying to develop a simple inventory system in VB.NET. I now have all the database set up and would like to know if there is any code that I can refer to that alert users when the stock is low (e.g pop out window informing users that stock is low or change the number of stock in the database table into different colours.

I have tried this code but it starts to highlight all the cumulus. I want to highlight a specific cell which is called Quantity so when the quantity is lower then 10 i want that quantity cell to get highlighted red.

For i As Integer = 0 To Me.ProductsDataGridView.Rows.Count - 1
    If Me.ProductsDataGridView.Rows(i).Cells(0).Value < 10 Then
       Me.ProductsDataGridView.Rows(i).Cells(0).Style.BackColor =  Color.Red
            MsgBox("Low on stock")
        End If
    Next

I will really appreciate any advice Thank You.

3

2 Answers 2

0

You can use

DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Red

but it will not get updated when you click on the column header

So better to use

Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then ' should take only 0 and greater than 0 -1 reffers to column header
        If e.ColumnIndex = 0 Then 'column want to compare
            If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value < 10 Then ' where count is less than ten
                DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Red ' if less than 10 change colour here
            End If
        End If
    End If
End Sub

now whatever you do the datagridview retain its colour even after refresh , and on update.

1
  • Thank you very much
    – adam
    Commented Mar 10, 2017 at 20:55
0

You don't need to iterate with an integer first.

You can set it up like this:

   For Each row As DataGridViewRow In ProductsDataGridView.Rows
            If row.Cells("yourcolumnname").Value < 10 Then
                row.Cells("yourcolumnname").Style.BackColor = Color.Red
            End If
   Next

This will go through each row and highlight the cell accordingly if it less than your desired amount.

2
  • Thank you very much
    – adam
    Commented Mar 10, 2017 at 20:55
  • No problem. If the answer suits you please mark it as answered
    – James N
    Commented Mar 13, 2017 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.