Change the row color in a DataGridView based on the number of cell values

I need to change the color of a row in a datagridview, but my code does not work for me. I always get the error "Column named Quantity: cannot be found. Parameter name: columnName"

Here is my code:

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1 If Me.DataGridView1.Rows(i).Cells("Quantity:").Value < 5 Then Me.DataGridView1.Rows(i).Cells("Quantity:").Style.ForeColor = Color.Red End If Next End Sub 

Please help me fix this. Thanks.

+7
source share
7 answers

I fixed my mistake. just deleted the "Value" from this line:

 If drv.Item("Quantity").Value < 5 Then 

So it will look like

If drv.Item("Quantity") < 5 Then

+2
source

It may be useful.

  • Use the "RowPostPaint" Event
  • The column name is NOT the "header" of the column. You need to go to the DataGridView => properties, then select the column => and then look for the "Name" property

I converted this from C # ('From: http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=74 )

  Private Sub dgv_EmployeeTraining_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgv_EmployeeTraining.RowPostPaint If e.RowIndex < Me.dgv_EmployeeTraining.RowCount - 1 Then Dim dgvRow As DataGridViewRow = Me.dgv_EmployeeTraining.Rows(e.RowIndex) '<== This is the header Name 'If CInt(dgvRow.Cells("EmployeeStatus_Training_e26").Value) <> 2 Then '<== But this is the name assigned to it in the properties of the control If CInt(dgvRow.Cells("DataGridViewTextBoxColumn15").Value.ToString) <> 2 Then dgvRow.DefaultCellStyle.BackColor = Color.FromArgb(236, 236, 255) Else dgvRow.DefaultCellStyle.BackColor = Color.LightPink End If End If End Sub 
+11
source

Try it (Note: I don't have Visual Studio right now, so the code is to copy the paste from my archive (I have not tested it):

 Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting Dim drv As DataRowView If e.RowIndex >= 0 Then If e.RowIndex <= ds.Tables("Products").Rows.Count - 1 Then drv = ds.Tables("Products").DefaultView.Item(e.RowIndex) Dim c As Color If drv.Item("Quantity").Value < 5 Then c = Color.LightBlue Else c = Color.Pink End If e.CellStyle.BackColor = c End If End If End Sub 
+1
source

Just delete : in Quantity . Make sure your attribute matches the parameter that you specify in the code, for example:

 Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1 If Me.DataGridView1.Rows(i).Cells("Quantity").Value < 5 Then Me.DataGridView1.Rows(i).Cells("Quantity").Style.ForeColor = Color.Red End If Next End Sub 
+1
source
 If drv.Item("Quantity").Value < 5 Then 

use it to like it

 If Cint(drv.Item("Quantity").Value) < 5 Then 
0
source

Using the CellFormating event and the e argument:

If CInt(e.Value) < 5 Then e.CellStyle.ForeColor = Color.Red

0
source
 Dim dgv As DataGridView = Me.TblCalendarDataGridView For i As Integer = 0 To dgv.Rows.Count - 1 For ColNo As Integer = 4 To 7 If Not dgv.Rows(i).Cells(ColNo).Value Is DBNull.Value Then dgv.Rows(i).Cells(ColNo).Style.BackColor = vbcolor.blue End If Next Next 
0
source

All Articles