In Delphi, How can I change the color of grid lines in a TDBGrid?
Asked Answered
V

1

7

I am using a TDBGrid component in a Delphi application, when I change rows colors the grid lines became unclear or almost invisible.

So, can any one show us how to change the color of the grid lines?

I mean: how to change the color of cells borders(see next picture)

The cells borders

Vizard answered 30/12, 2018 at 22:1 Comment(11)
Do you mean the cell borders? Font color? Background color?Ruffina
I mean cells bordersVizard
Please post your code and edit your question to be more specific about what exactly your trying to do.Ruffina
JohnEasley , see the picture it could help you to understand my questionVizard
Why the downvotes? The expression "color of grid lines" is perfectly clearTommietommy
@TomBrunberg: Not sure about that, some would write 'lines' when they actually mean 'rows'. Not my dv, btw, but I'm not sure the q merits the upvotes either.Embolectomy
Which Delphi version?Embolectomy
@Embolectomy Delphi itself says grid lines in the meaning intended here, and rows for ... well rows. E.g. property grid line width. So I guess one could assume "official" terminology as default, at least before downvotingTommietommy
@TomBrunberg: Fair enough, I wasn't meaning to argue.Embolectomy
@Embolectomy Me neither. HNY btw.Tommietommy
@Embolectomy , I am using Delphi 10.2Vizard
R
11

Are you looking for

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
Var
  R: TRect;
begin
  R:= Rect;
  with DBGrid1.Canvas do
    begin
      Brush.Color:= clRed;
      R.Offset(Column.Width, 0);
      FillRect(R);
      R:= System.Types.Rect(Rect.Left, Rect.Bottom - 1, Rect.Right, Rect.Bottom);
      FillRect(R);
    end;
end;

The results will be like:

A better way (from Tom Brunberg comment) is to use FrameRect() as

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  with DBGrid1.Canvas do
    begin
      Brush.Color:= clRed;
      FrameRect(Rect);
    end;
end;

Use FrameRect() to draw a 1 pixel wide border around a rectangular region, which does not fill the interior of the rectangle with the Brush pattern. To draw a boundary using the Pen instead, use the Polygon method

Rodin answered 31/12, 2018 at 9:27 Comment(1)
Instead of twice FillRect() I would prefer FrameRect() or Polygon()Tommietommy

© 2022 - 2024 — McMap. All rights reserved.