Change text color in datagrid (windows form) depending on value of other column


I have a datagrid where I display some columns.  This datagrid is based on a dataset, one of the columns of this dataset is Color, where the numerical value of the color is stored (e.g. 255 for red).  My question now is : how can I set the fontcolor of my columns to that stored column.  I have already created a class 'ColoredDataGridColumn' that I inherited from DataGridTextBoxColumn, and I created a sub Paint in the class:
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)

My problem is now : how do I know the color that is stored in the column ?

I already examined the code that changes the background to pink if the first character is bigger than "F", and this works, but this is a very static test.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Can you please give some more examples of some numerical values of the color that you want...
I never tried to do exactly this, but since you've based your column as a TextBoxColumn, you might have a MyBase.TextBox.Text .

That's where I expect your color value to be.
ACAEAuthor Commented:
Hi Juravich,

The color itself is not the problem, I can convert a color as 255 to with ColorTranslator.FromWin32(255). The problem is to know the value of the color when painting another textbox
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Bob LearnedCommented:
You can get the color from the brush.

ACAEAuthor Commented:
I know how to set the color of each colomn if I use a fixed test (e.g. value < 0), but not if the color depends on the value of another column.
Bob LearnedCommented:
What are you asking for?

ACAEAuthor Commented:
I have a dataset which contains several columns, one is called 'fontcolor' and contains a numeric value for the color (e.g. 255 =

I have a datagrid based on this dataset, in which I display several fields (not fontcolor, other fields). I want the value of the fields (datagridcolumn) to be displayed in the color that is stored in 'fontcolor'. The problem is not to convert 255 to a systemcolor, but how to know the value of 'fontcolor' in the 'paint' event of the datagridcolumn (actually a descendant of datagridcolumn).

I have a set of orders, and each order has a specific status (created, picked, shipped, ...), and each status has a color (the user can choose this color). So when I show a list of the orders, I want each order (=1 line in datagrid) to be displayed in the color of the orderstatus .
Bob LearnedCommented:
Are you asking for a way to associate the "FontColor' value column with the actual colored DataGrid column?

ACAEAuthor Commented:
Yes, I want a way to 'color' the datagrid column text with the color specified in another column
Bob LearnedCommented:
Here is what I have for a colored DataGrid column:

Public Class DataGridColorTextBoxColumn
  Inherits DataGridTextBoxColumn

  Private m_maxColumn As DataGridColorTextBoxColumn
  Public Property MaxValueColumn() As DataGridColorTextBoxColumn
      Return m_maxColumn
    End Get
    Set(ByVal Value As DataGridColorTextBoxColumn)
      m_maxColumn = Value
    End Set
  End Property

  Private m_source As CurrencyManager

  Private Function GetRowValue(ByVal rowNumber As Integer) As String
    Return Me.GetColumnValueAtRow(m_source, rowNumber)
  End Function

  Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
   ByVal bounds As Rectangle, ByVal source As CurrencyManager, _
   ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)


      If Me.MaxValueColumn Is Nothing Then
        Throw New ArgumentException("Max value column is not set.")
      End If

      m_source = source

      Dim columnValue As Double = Me.GetRowValue(rowNum)

      If columnValue > Me.MaxValueColumn.GetRowValue(rowNum) Then
        backBrush = New SolidBrush(Color.Green)
        foreBrush = New SolidBrush(Color.White)
      End If

    Catch ex As Exception



      MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

    End Try

  End Sub

End Class

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
In this case, I was setting MaxValueColumn to another DataGridTextBoxColumn that had a value that I needed to test against.  In the Paint event, I get the value from the cell, and compare it against the MaxValueColumn's value to define different brushes for the cell.

ACAEAuthor Commented:
Hi Bob,

I got it working, thx a lot for your help !

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.