Have you considered then using CellFormatting event to change the desired colors?
Try the following, see if it will work for you, requires a DataGridView on a form with the following code. A string and Date column are formatted in the CellFormat event.
Code:Public Class Form2 WithEvents bsData As New BindingSource Private Sub DataGridView1_CellFormatting( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _ Handles DataGridView1.CellFormatting If e.ColumnIndex = DataGridView1.Columns("Column2").Index Then If e.Value.ToString = "2" Then e.CellStyle.BackColor = Color.Crimson e.CellStyle.ForeColor = Color.White End If End If If e.ColumnIndex = DataGridView1.Columns("Column3").Index Then If CType(e.Value, DateTime).Month > Now.Month Then e.CellStyle.BackColor = Color.Green e.CellStyle.ForeColor = Color.White End If End If End Sub Private Sub Form2_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load bsData.DataSource = MockData() DataGridView1.AllowUserToAddRows = False DataGridView1.DataSource = bsData DataGridView1.Columns("Column3").DefaultCellStyle.Format = "d" End Sub Private Function MockData() As DataTable Dim dt As New DataTable With {.TableName = "MyTable"} dt.Columns.AddRange(New DataColumn() _ { _ New DataColumn("Column1", GetType(System.String)), _ New DataColumn("Column2", GetType(System.String)), _ New DataColumn("Column3", GetType(System.DateTime)) _ } _ ) dt.Rows.Add(New Object() {"A", "1", Now}) dt.Rows.Add(New Object() {"B", "2", Now.AddMonths(1)}) dt.Rows.Add(New Object() {"C", "3", Now}) dt.Rows.Add(New Object() {"D", "2", Now.AddMonths(2)}) dt.Rows.Add(New Object() {"E", "5", Now}) dt.Rows.Add(New Object() {"F", "6", Now.AddMonths(3)}) dt.AcceptChanges() Return dt End Function End Class




Reply With Quote