• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

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

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
0
ACAE
Asked:
ACAE
1 Solution
 
juravichCommented:
Can you please give some more examples of some numerical values of the color that you want...
0
 
PockyMasterCommented:
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.
0
 
ACAEAuthor 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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Bob
0
 
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.
0
 
Bob LearnedCommented:
What are you asking for?

Bob
0
 
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 = 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 .
0
 
Bob LearnedCommented:
Are you asking for a way to associate the "FontColor' value column with the actual colored DataGrid column?

Bob
0
 
ACAEAuthor Commented:
Yes, I want a way to 'color' the datagrid column text with the color specified in another column
0
 
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
    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
0
 
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.

Bob
0
 
ACAEAuthor Commented:
Hi Bob,

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

Andy
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now