设置DBGrid当前行的颜色
定义个子类MyDBGrid继承TDBGrid,以提供访问父类保护成员的接口。
type
MyDBGrid=class(TDBGrid)
private
public
function GetRow:integer;
function IsCurrent:boolean;
end;
function MyDBGrid.GetRow: integer;
begin
result:=row;
end;
function MyDBGrid.IsCurrent: boolean;
begin
result:=(DataLink.ActiveRecord=row-1);
end;
判断是否是当前行。
type
TGlobal=class
class function IsCurrentRow(AMyDBGrid:MyDBGrid):boolean;
end;
class function TGlobal.IsCurrentRow(AMyDBGrid: MyDBGrid): boolean;
begin
result:=AMyDBGrid.IsCurrent;
end;
在DrawColumnCell事件中。
procedure TFormMain.MyDBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if TGlobal.IsCurrentRow(MYDBGrid(Sender))
begin
(Sender as TDBGrid).Canvas.Brush.Color :=$000080FF;
end
else
begin
if (Sender as TDBGrid).DataSource.DataSet.RecNo mod 2 =0 then
(Sender as TDBGrid).Canvas.Brush.Color :=rgb(246,246,246); //$00F3F3F3; //定义背景颜色
end;
(Sender as TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
效果: