根据条件使DBGrid不同行显示不同的颜色

字体大小: 中小 标准 ->行高大小: 标准
下面的内容可以使DBGrid不同行显示不同的颜色,这里采用的使偶数行显示红色的字体和黄色的背景,条件部分可以更换为您使用中的条件,新建一个Application,然后放入Table,DataSource、DBGrid组件,然后将Table和数据库联系起来,并把其他的组件相互之间联系起来,然后在写入下面的代码:

procedure TForm1.FormCreate(Sender: TObject);
begin
Table1.Open;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Table1.Close;
end;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if DBGrid1.DataSource.DataSet.RecNo mod 2 = 0 then //条件判断
begin
with DBGrid1 do
begin
Canvas.Font.Color:=clRed;
Canvas.Brush.Color:=clYellow;
end;
end;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;  

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/68120.html