procedure TForm1.ColoredDBGrid1 DRawColoredDBGrid
(Sender: TObject; Field: TField; var Color:
TColor; var Font: TFont);
Var
p : Integer;
begin
p := Table1.FindField('wage').AsInteger;
//取得当前记录的Wage字段的值。
if(p < 500) then begin
//程序将根据wage值设置各行的颜色。
Color := clGreen;
Font.Style := [fsItalic];
//不仅可以改变颜色,还可以改变字体
end;
if(p >= 500) And (p < 800) then
Color := clRed;
if(p >=800) then begin
Color := clMaroon;
Font.Style := [fsBold];
end;
end;
//用‘退出’按钮结束程序运行。
procedure TForm1.Button1Click(Sender: TObject);
begin
Close;
end;
在DBGrid中的DrawColumnCell事件中写:
if DataMod.QryContent.RecordCount>0 then
begin
if DataMod.QryContent.FieldByName('ISPASSED').AsInteger=0 then
(Sender as TDBGrid).Canvas.Brush.Color:=clMoneyGreen;
(Sender as TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
改变颜色。