Link to home
Create AccountLog in
Avatar of ACAE
ACAE

asked on

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
Avatar of juravich
juravich

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.
Avatar of ACAE

ASKER

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
Avatar of Bob Learned
You can get the color from the brush.

Bob
Avatar of ACAE

ASKER

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.
What are you asking for?

Bob
Avatar of ACAE

ASKER

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

Bob
Avatar of ACAE

ASKER

Yes, I want a way to 'color' the datagrid column text with the color specified in another column
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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
Avatar of ACAE

ASKER

Hi Bob,

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

Andy