procedure TFrmuser.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if gdSelected in State then begin (Sender as TDBGrid).Canvas.Brush.Color :=$000080FF; //当前选中单元格显示红色 DBGrid1.DefaultDrawColumnCell (Rect,DataCol,Column,State); end else begin //隔行改变网格背景色: if DBGrid1.DataSource.DataSet.RecNo mod 2 = 0 then (Sender as TDBGrid).Canvas.Brush.Color := $00EFF1FC //定义背景颜色 else (Sender as TDBGrid).Canvas.Brush.Color := $00F3EADE; //定义背景颜色 DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); end; end;
先将DBGRID的options的dgselectRow属性设置为true! procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if state=[gdSelected] then //这是指被选择的行 begin DBGrid1.Canvas.Font.Color:=clyellow; DBGrid1.Canvas.Brush.Color:=clred; //重新绘制这一行 DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); end; end;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if gdSelected in State then
begin
(Sender as TDBGrid).Canvas.Brush.Color :=$000080FF; //当前选中单元格显示红色
DBGrid1.DefaultDrawColumnCell (Rect,DataCol,Column,State);
end else
begin
//隔行改变网格背景色:
if DBGrid1.DataSource.DataSet.RecNo mod 2 = 0 then
(Sender as TDBGrid).Canvas.Brush.Color := $00EFF1FC //定义背景颜色
else
(Sender as TDBGrid).Canvas.Brush.Color := $00F3EADE; //定义背景颜色
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if state=[gdSelected] then
//这是指被选择的行
begin
DBGrid1.Canvas.Font.Color:=clyellow;
DBGrid1.Canvas.Brush.Color:=clred;
//重新绘制这一行
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin 裡面是你的條件end;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if DBGrid1.DataSource.DataSet.FieldByName('字段名').AsString = 'AA' then //条件
(Sender as TDBGrid).Canvas.Brush.Color := $00EFF1FC //定义背景颜色
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;