We help IT Professionals succeed at work.

Coloring dbgrid rows when it meets one or more condition

DigitalNam
DigitalNam asked
on
Medium Priority
351 Views
Last Modified: 2012-05-11
Related to my previous question.

https://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26977229.html

Is there a way to color certain coloumns a different color depending on the value of the field?

The first column is Doc_Type and I need to have it colored in 3 different colors depending the value eg: If value is Fax then Row is Red if value is Original = Green and if value is Copy = Yellow.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks JimyX. At the rate I am going today you are going to earn ALOT of points. Hehe
CERTIFIED EXPERT
Top Expert 2011

Commented:
Could you try this please:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
    DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  DBGrid1.Canvas.Brush.Color := clWhite;  // the default color

  if DB.FieldByName('Doc_Type').AsString = 'Fax' then //you can use uppercase() to handle case sensitivity, if required
    DBGrid1.Canvas.Brush.Color := clRed;
  if DB.FieldByName('Doc_Type').AsString = 'Original' then
    DBGrid1.Canvas.Brush.Color := clGreen;
  if DB.FieldByName('Doc_Type').AsString = 'Copy' then
    DBGrid1.Canvas.Brush.Color := clYellow;
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

Open in new window

CERTIFIED EXPERT
Top Expert 2011

Commented:
Am ready and cracking my fingers waiting  :)

Author

Commented:
Get ready for the next question.. :-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

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