troubleshooting Question

Coloring in DBGrid

Avatar of Tomas Helgi Johannsson
Tomas Helgi JohannssonFlag for Iceland asked on
6 Comments2 Solutions514 ViewsLast Modified:

I have a DB grid where I color the lines after sertain status.
But if I use yellow or some other color the text disapears or doesn't show
when I have a row selected. I want to have it colored in red or some other color than
the background color.

How do I do that. ?

The code is similar to this code which I took from the TIPS at

procedure TForm1.ColorGrid(dbgIn: TDBGrid; qryIn: TQuery; const Rect: TRect;
  DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
  iValue: LongInt;
  // color only the first field
  // nur erstes Feld einfärben
  if (DataCol = 0) then
    // Check the field value and assign a color
    // Feld-Wert prüfen und entsprechende Farbe wählen
    iValue := qryIn.FieldByName('HINWEIS_COLOR').AsInteger;
    case iValue of
      1: dbgIn.Canvas.Brush.Color := clGreen;
      2: dbgIn.Canvas.Brush.Color := clLime;
      3: dbgIn.Canvas.Brush.Color := clYellow;
      4: dbgIn.Canvas.Brush.Color := clRed;
    // Draw the field
    // Feld zeichnen
    dbgIn.DefaultDrawColumnCell(Rect, DataCol, Column, State);

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  ColorGrid(DBGrid1, Query1, Rect, DataCol, Column, State);

    Tomas Helgi
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros