如下程序 procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:TRect;DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if Table1.FieldByName('Population').AsInteger > 20000000 then DBGrid1.Canvas.Font.Color := clBlue; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end;
将DBGrid的Options中的dgRowSelect设为True; 用以下的过程即可!其实这些语句写在DrawDataCell和DrawColumnCell中都可以,只有最后一句不一样。 procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin if gdselected in State then//必须用gdselected with DBGrid1.Canvas do begin Brush.Color:=clred;//背景色 Font.Color:=clWhite;//字体颜色 DBGrid1.DefaultDrawDataCell(Rect,Field,State); //如果在DrawColumnCell事件中,用下面的语句。 //DBGrid1.DefaultDrawDataCell(Rect,Column.Field,State); end; end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:TRect;DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('Population').AsInteger > 20000000 then
DBGrid1.Canvas.Font.Color := clBlue;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
用以下的过程即可!其实这些语句写在DrawDataCell和DrawColumnCell中都可以,只有最后一句不一样。
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if gdselected in State then//必须用gdselected
with DBGrid1.Canvas do
begin
Brush.Color:=clred;//背景色
Font.Color:=clWhite;//字体颜色
DBGrid1.DefaultDrawDataCell(Rect,Field,State);
//如果在DrawColumnCell事件中,用下面的语句。
//DBGrid1.DefaultDrawDataCell(Rect,Column.Field,State);
end;
end;