怎样改变DBGRID的每行的颜色?(50分)

  • 主题发起人 主题发起人 soit
  • 开始时间 开始时间
写dbgrid1.OnDrawColumnCell事件.
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
s: string;
begin
with DbGrid1.Canvas do
begin
brush.Style := BsSolid;
Brush.Color := clRed;
Font := DbGrid1.Font;
FillRect(Rect);
s := GetFieldValue(DataCol);
TextOut(Rect.Left + 2, Rect.Top + 2, s);
end;
end;
 
方法很多的!如:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.RecNo mod 2=0 then
DBGrid1.Color:=clred
else
DBGrid1.Color:=clblue;
end;
 
procedure TForm1.DBGrdTypeDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
inherited;
with DBGrid1 do
begin
if State <> [gdSelected, gdFocused] then
begin
if xxxx then
Canvas.Font.Color := clBlue
else
Canvas.Font.Color := clRed
end;
DefaultDrawColumnCell(Rect, DataCol, Column, State); //这行是关键
end;
end;
 
xixi 的例子改的是字体颜色, 如果要调整底色用 Canvas.brush.color ;

if xxxx then //判别条件 例如 对应记录的某个值大于零
Canvas.brush.Color := clBlue
else
Canvas.brush.Color := clRed;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
I
回复
0
查看
320
import
I
后退
顶部