【Delphi】以不同顏色顯示 DBGrid 目前所在列

DBGridOptions 屬性內有個 dgRowSelect 的選項,當其值為 true 時,DBGridindicator 所在那一列會以藍色底色,白色字顯示。

但這有個缺點 -- 當欄位數超過 DBGrid 寬度時,就無法以方向鍵的向左/右鍵去其他欄位並且無法修改個別欄位。

所以要另外拐個彎處理......

type
    THackDBGrid = class(TDBGrid)
    end;

    TForm1 = class(TForm)
      ....
    end;


procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
    if THackDBGrid(DBGrid1).Row=(1+THackDBGrid(DBGrid1).DataLink.ActiveRecord) then
    begin
        with DBGrid1.Canvas do
        begin
            Brush.Color := clSilver;
            Font.Color := clNavy;
        end;
    end;
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
end;