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

妖精的绣舞 提交于 2021-01-27 06:46:24

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/53981784/in-delphi-how-can-i-change-the-color-of-grid-lines-in-a-tdbgrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!