var field : TField; //定义一个字段型变量 dataset : TDataSet; //定义一个DataSet important : string; //定义一个字符串型变量 begin dataset := DBGrid1.DataSource.DataSet ; //获得DBGrid控件的父类的DataSet属性 field := dataset.FieldByName('content_id'); //获取相应的字段 important := field.asString; //获得相应字段的字符串型的数据值 if important='A' then DBGrid1.canvas.font.color := clRed; if important='C' then DBGrid1.canvas.font.color := clBlue; //判断值的内容,进而根据相应相应的条件设置该行的颜色 DBGrid1.DefaultDrawColumnCell(rect,DataCol,column,state); //使颜色的改变生效
参考下面的 procedure TEmp_F.dbgEmpGetCellParams(Sender: TObject; Column: TColumnEh; AFont: TFont; var Background: TColor; State: TGridDrawState); begin if dbgemp.sumlist.RecNo mod 2 = 1 then begin Background := MyGridColor1; end else begin Background := MyGridColor2; end; stat1.Panels[0].Text := '记录数:' + IntToStr(qryEmp.RecordCount); //////自动用颜色提醒一些事情 /// 合同到期提醒 if HTTX_B then begin if (qryEmp.FieldByName('ht2').AsString > '') and (qryEmp.FieldByName('ht2').AsDateTime < Now + HTTX_I) then begin AFont.Color := clRed; end; end; ///身份证到期提醒 if SFZTX_B then begin if (qryEmp.FieldByName('SFZd2').AsString > '') and (qryEmp.FieldByName('SFZd2').AsDateTime < Now + SFZTX_I) then begin AFont.Color := clRed; end; end; ///转正提醒 if ZZTX_B then begin if (qryEmp.FieldByName('syq').AsString > '') and (IncMonth(qryEmp.FieldByName('bdrq').AsDateTime, qryEmp.FieldByName('syq').AsInteger) < Now + ZZTX_I) then begin AFont.Color := clRed; end; end; end;
刚开始没看清楚,还以为是行~~试试这个 procedure TMainForm.BitBtn1Click(Sender: TObject); begin DbGrid1.Columns[0].Color:= clLime; DbGrid1.Columns[0].Font.Color:= clYellow; end;
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;Field: TField; State: TGridDrawState); begin if Table1.Fieldbyname(′Salary′).value<=SpinEdit1.value then DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor else DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor; DBGrid1.Canvas.FillRect(Rect); DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString); end; 这个过程的作用是当SpinEdit1给定的条件得以满足时,如′salary′变量低于或等于SpinEdit1.Value时,DBGrid1记录以ColorGrid1的前景颜色来显示,否则以ColorGrid1的背景颜色来显示。然后调用DBGrid的Canvas的填充过程FillRect和文本输出过程重新绘制DBGrid的画面。这个是我几年前收集的 你看看
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var i:integer; mycolor:TColor; begin for i:=1 to aCount do begin mycolor:=i*10+i*21+3; if ADOQuery1.FieldByName('CarNo').AsString=strs[i-1] then begin DBGrid1.Canvas.Font.Color:=mycolor; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end; if GdSelected in State then begin exit; end; end; end;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
field : TField;
//定义一个字段型变量
dataset : TDataSet;
//定义一个DataSet
important : string;
//定义一个字符串型变量
begin
dataset := DBGrid1.DataSource.DataSet ;
//获得DBGrid控件的父类的DataSet属性
field := dataset.FieldByName('content_id');
//获取相应的字段
important := field.asString;
//获得相应字段的字符串型的数据值
if important='A' then DBGrid1.canvas.font.color := clRed;
if important='C' then DBGrid1.canvas.font.color := clBlue;
//判断值的内容,进而根据相应相应的条件设置该行的颜色
DBGrid1.DefaultDrawColumnCell(rect,DataCol,column,state);
//使颜色的改变生效
end;
DBGrid1.Columns[0].Font.Color := clRed;
类似
procedure TEmp_F.dbgEmpGetCellParams(Sender: TObject; Column: TColumnEh;
AFont: TFont; var Background: TColor; State: TGridDrawState);
begin
if dbgemp.sumlist.RecNo mod 2 = 1 then
begin
Background := MyGridColor1;
end else
begin
Background := MyGridColor2;
end; stat1.Panels[0].Text :=
'记录数:' + IntToStr(qryEmp.RecordCount); //////自动用颜色提醒一些事情
/// 合同到期提醒
if HTTX_B then
begin
if (qryEmp.FieldByName('ht2').AsString > '') and
(qryEmp.FieldByName('ht2').AsDateTime < Now + HTTX_I) then
begin
AFont.Color := clRed;
end;
end; ///身份证到期提醒
if SFZTX_B then
begin
if (qryEmp.FieldByName('SFZd2').AsString > '') and
(qryEmp.FieldByName('SFZd2').AsDateTime < Now + SFZTX_I) then
begin
AFont.Color := clRed;
end;
end; ///转正提醒
if ZZTX_B then
begin
if (qryEmp.FieldByName('syq').AsString > '') and
(IncMonth(qryEmp.FieldByName('bdrq').AsDateTime,
qryEmp.FieldByName('syq').AsInteger) < Now + ZZTX_I) then
begin
AFont.Color := clRed;
end;
end;
end;
procedure TMainForm.BitBtn1Click(Sender: TObject);
begin
DbGrid1.Columns[0].Color:= clLime;
DbGrid1.Columns[0].Font.Color:= clYellow;
end;
begin
if Table1.Fieldbyname(′Salary′).value<=SpinEdit1.value then
DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor
else
DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor;
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString);
end; 这个过程的作用是当SpinEdit1给定的条件得以满足时,如′salary′变量低于或等于SpinEdit1.Value时,DBGrid1记录以ColorGrid1的前景颜色来显示,否则以ColorGrid1的背景颜色来显示。然后调用DBGrid的Canvas的填充过程FillRect和文本输出过程重新绘制DBGrid的画面。这个是我几年前收集的 你看看
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
i:integer;
mycolor:TColor;
begin
for i:=1 to aCount do
begin
mycolor:=i*10+i*21+3;
if ADOQuery1.FieldByName('CarNo').AsString=strs[i-1] then
begin
DBGrid1.Canvas.Font.Color:=mycolor;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
if GdSelected in State then
begin
exit;
end;
end;
end;