贴一段,这样的代码很多。搜一下就有。 procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if Table1.FieldByName('size').AsInteger >=40 then begin DBGrid1.Canvas.Font.Color := clRed; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end; if (Table1.FieldByName('size').AsInteger >=30) and (Table1.FieldByName('size').AsInteger<40) then begin DBGrid1.Canvas.Font.Color := clBlue; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end; if (Table1.FieldByName('size').AsInteger >=20) and (Table1.FieldByName('size').AsInteger<30) then begin DBGrid1.Canvas.Font.Color := clYellow; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end; if (Table1.FieldByName('size').AsInteger >=10) and (Table1.FieldByName('size').AsInteger<20) then begin DBGrid1.Canvas.Font.Color := clLime; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end; end;end.
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if adoquery1.RecNo =10 then DbGrid1.Canvas.Brush.color:=rgb(0,0,125); DbGrid1.Canvas.pen.mode:=pmMask; DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State); end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('size').AsInteger >=40 then
begin
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
if (Table1.FieldByName('size').AsInteger >=30) and (Table1.FieldByName('size').AsInteger<40) then
begin
DBGrid1.Canvas.Font.Color := clBlue;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
if (Table1.FieldByName('size').AsInteger >=20) and (Table1.FieldByName('size').AsInteger<30) then
begin
DBGrid1.Canvas.Font.Color := clYellow;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
if (Table1.FieldByName('size').AsInteger >=10) and (Table1.FieldByName('size').AsInteger<20) then
begin
DBGrid1.Canvas.Font.Color := clLime;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;end.
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if adoquery1.RecNo =10 then
DbGrid1.Canvas.Brush.color:=rgb(0,0,125);
DbGrid1.Canvas.pen.mode:=pmMask;
DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
end;
在dbgrid的DBGrid1DrawColumnCell事件中改颜色就可以
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
datacol 是行标
column是列标
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
dbgrid1.Canvas.Pen.Mode := pmmask;
if adoquery1.RecNo = 2 then //第2条记录,也是第2 行。
Canvas.brush.Color := ClYellow; //设定第2行背景为黄色
dbgrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); //绘制颜色。。
end;end.