We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

ACAE
ACAE asked
on
Medium Priority
387 Views
Last Modified: 2012-06-21
Hi,

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.

Thanx,
Andy
Comment
Watch Question

Commented:
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.

Author

Commented:
Hi Juravich,

The color itself is not the problem, I can convert a color as 255 to color.red with ColorTranslator.FromWin32(255). The problem is to know the value of the color when painting another textbox
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
You can get the color from the brush.

Bob

Author

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.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What are you asking for?

Bob

Author

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

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).

Basically:
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 .
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Are you asking for a way to associate the "FontColor' value column with the actual colored DataGrid column?

Bob

Author

Commented:
Yes, I want a way to 'color' the datagrid column text with the color specified in another column
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
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
    Get
      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)

    Try

      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

      MessageBox.Show(ex.ToString())

    Finally

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

    End Try


  End Sub

End Class

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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.

Bob

Author

Commented:
Hi Bob,

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

Andy
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.