Search code examples
delphicolorsdbgrid

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


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


Solution

  • 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