这是一个TStringGrid的例子 dbGrid和stringgrid是一样的,你试试效果 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids;type TForm1 = class(TForm) StringGrid1: TStringGrid; procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin if (acol=1) and (arow=2) then begin StringGrid1.Canvas.Font.Color:=clred; StringGrid1.Canvas.Brush.Color:=clyellow; StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,StringGrid1.Cells[Acol,arow]); end; end;procedure TForm1.FormCreate(Sender: TObject); var i,j : integer; begin StringGrid1.RowCount:=5; StringGrid1.ColCount:=5; for i:=0 to 4 do for j:= 0 to 4 do StringGrid1.Cells[i,j]:=inttostr(i*j); end;end.
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin if gdSelected in State then begin DBGrid1.Canvas.Brush.Color := clRed; DBGrid1.Canvas.FillRect(Rect); DBGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, Field.Value); end; end;
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin if (Table1.FieldByName('xx').AsString='A') and (Column.Field=Table1.fieldbyname('xx')) then begin DBGrid1.Canvas.Font.Color := clRed; DBGrid1.Canvas.TextOut(Rect.Left,Rect.Top,Table1.Fieldbyname('xx').AsString); end; end; 这样就不行吗
dbGrid和stringgrid是一样的,你试试效果
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids;type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (acol=1) and (arow=2) then
begin
StringGrid1.Canvas.Font.Color:=clred;
StringGrid1.Canvas.Brush.Color:=clyellow;
StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,StringGrid1.Cells[Acol,arow]);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var
i,j : integer;
begin
StringGrid1.RowCount:=5;
StringGrid1.ColCount:=5;
for i:=0 to 4 do
for j:= 0 to 4 do
StringGrid1.Cells[i,j]:=inttostr(i*j);
end;end.
Field: TField; State: TGridDrawState);
begin
if gdSelected in State then
begin
DBGrid1.Canvas.Brush.Color := clRed;
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, Field.Value);
end;
end;
选择已经存在的项,将它的Color属性修改成你所需要的颜色即可。
if Table1.FieldByName('Size').AsFloat > 10 then
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
我也是这样作的
比较直接与方便
begin
if (Table1.FieldByName('xx').AsString='A') and (Column.Field=Table1.fieldbyname('xx')) then
begin
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.Canvas.TextOut(Rect.Left,Rect.Top,Table1.Fieldbyname('xx').AsString);
end;
end;
这样就不行吗