DataGridView filtered

26 Views Asked by At

I have to highlight the datagridview rows at runtime if the checkbox of the first column is flagged. I can't use a normal for each loop to color the rows because there are about 3000 of them and it would take too long.

I should directly filter the rows from the for each loop with the condition Column1 = True ... ... could you help me find the right synopsis: I tried with:

For Each selRows As DataGridViewRow In myDataGrid.SelectedColumns(0).Equals("Column1 = true")
  myDataGrid.Rows(selRows.index).DefaultCellStyle.BackColor = Color.LightGreen
Next

but it does not work

Thank you

1

There are 1 best solutions below

0
F0r3v3r-A-N00b On

You can use the CellPainting event

Private Sub dgv_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting
   '   check if the row is the header row
   If e.RowIndex = -1 Then
       Exit Sub
   End If
   '   cast the row to the correct data type, here I'll assume this is a datarow
   Dim row As DataRow = CType(dgv.Rows(e.RowIndex).DataBoundItem, DataRowView).Row
   '   perform your check 
   If CType(row("Column1"), Boolean) Then
       dgv.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen
   End If
End Sub