procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with TDBGrid(Sender).Canvas do
begin
if (gdFocused in State) or (gdSelected in State) then
begin
Brush.Color := clYellow;
Font.Color := clBlack;
end;
FillRect(Rect);
TextOut(Rect.Left+2, Rect.Top+2, Column.Field.AsString);
Brush.Style := bsSolid;
end;
end;
// 아래 예제는 선택된 컬럼의 타이틀과 필드를 다른 색으로 나타내는것입니다
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, Grids, DBGrids, DBTables;
type
TForm1 = class(TForm)
Table1: TTable;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
procedure DBGrid1ColEnter(Sender: TObject);
procedure DBGrid1ColExit(Sender: TObject);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
// 선택된 필드의 title 색을 파란색으로 굵게 ...
DBGrid1.Columns[DBGrid1.SelectedIndex].Title.Font.Color := clRed;
DBGrid1.Columns[DBGrid1.SelectedIndex].Title.Font.Style :=
DBGrid1.Columns[DBGrid1.SelectedIndex].Title.Font.Style + [fsBold];
DBGrid1.Repaint;
end;
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
// 원래대로...
DBGrid1.Columns[DBGrid1.SelectedIndex].Title.Font.Color := clBlack;
DBGrid1.Columns[DBGrid1.SelectedIndex].Title.Font.Style :=
DBGrid1.Columns[DBGrid1.SelectedIndex].Title.Font.Style - [fsBold];
DBGrid1.Repaint;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with TDBGrid(Sender).Canvas do
begin
if (gdFocused in State) or (gdSelected in State) then
begin
Brush.Color := clYellow;
Font.Color := clBlack;
end;
FillRect(Rect);
TextOut(Rect.Left+2, Rect.Top+2, Column.Field.AsString);
Brush.Style := bsSolid;
end;
end;
end.