apply color to cell through code

The CellFormatting event occurs when the contents of a cell in a Windows Forms DataGridView control needs to be formatted for display.

To set the BackColor of a cells based on their values, create a CellFormatting event handler. Within the handler add code to set the BackColor of individual cells based on their value.


The CellFormatting event occurs every time each cell is painted, so you should avoid lengthy processing when handling this event.



Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles BoatActive_DataGridView.CellFormatting
Dim a As String = Me.BoatActive_DataGridView.Columns(e.ColumnIndex).Name
If Me.BoatActive_DataGridView.Columns(e.ColumnIndex).Name = "active" Then
If e.Value IsNot Nothing Then
' If the value of the cell is "Inactive" AND this form's inactiveCustomersCheckBox control is checked..
If e.Value.ToString = "True" Then
' Set the BackColor of the cell to yellow.
'e.CellStyle.BackColor = Color.Blue
Else
'e.CellStyle.BackColor = Color.Red
End If
End If
End If
End Sub


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: