2012-08-01

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

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

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

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

  1.  
  2. type
  3. THackDBGrid = class(TDBGrid)
  4. end;
  5.  
  6. TForm1 = class(TForm)
  7. ....
  8. end;
  9.  
  10.  
  11. procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  12. begin
  13. if THackDBGrid(DBGrid1).Row=(1+THackDBGrid(DBGrid1).DataLink.ActiveRecord) then
  14. begin
  15. with DBGrid1.Canvas do
  16. begin
  17. Brush.Color := clSilver;
  18. Font.Color := clNavy;
  19. end;
  20. end;
  21. DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
  22. end;
  23.  


沒有留言:

張貼留言