关于DBGrid颜色问题 ( 积分: 100 )

  • 主题发起人 主题发起人 hying95
  • 开始时间 开始时间
H

hying95

Unregistered / Unconfirmed
GUEST, unregistred user!
比如有如下字段:
字段1 字段2 字段3 字段4...
10 25 5 65
15 35 20 19
45 14 26 73
....
我这个网格中这么一些数字,如何能把可以被5整除的数的那一格变成红色?
如果有若干条,我只把能看到变色?如何做?
 
我的DBGrid有个事件
procedure TFrmLH.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
i: integer;
begin
if gdSelected in State then Exit;
//定义表头的字体和背景颜色:
for i :=0 to (Sender as TDBGrid).Columns.Count-1 do
begin
(Sender as TDBGrid).Columns.Title.Font.Name :='宋体'; //字体
(Sender as TDBGrid).Columns.Title.Alignment:= taCenter;
(Sender as TDBGrid).Columns.Alignment:=taCenter;
(Sender as TDBGrid).Font.Name :='宋体';
(Sender as TDBGrid).Font.Size :=11;
(Sender as TDBGrid).Columns.Title.Font.Size :=12; //字体大小
(Sender as TDBGrid).Columns.Title.Font.Color :=clblue;//字体颜色(红色)
(Sender as TDBGrid).Columns.Title.Color :=clinactivecaptiontext;//背景色(绿色)
end;
//隔行改变网格背景色:
if table1.RecNo mod 2 = 0 then
(Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //定义背景颜色
else
(Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定义背景颜色
//定义网格线的颜色:
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
with (Sender as TDBGrid).Canvas do //画 cell 的边框
begin
Pen.Color := $00ff0000; //定义画笔颜色(蓝色)
MoveTo(Rect.Left, Rect.Bottom); //画笔定位
LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线
Pen.Color := clWindowtext;//$0000ff00; //定义画笔颜色(绿色)
MoveTo(Rect.Right, Rect.Top); //画笔定位
LineTo(Rect.Right, Rect.Bottom); //画绿色的竖线
end;
end;
 
看来有点难!
 
帮你在网上搜出来一段代码和你的意
链接:http://www.builder.com.cn/2007/1031/594622.shtml
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if Table1.FieldByName(’Salary’).AsCurrency>40000 then
begin
DBGrid1.Canvas.Font.Color:=clWhite;
DBGrid1.Canvas.Brush.Color:=clBlack;
end;
if DataCol = 4 then
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
 
用pos()应该可以实现
 
接受答案了.
 
后退
顶部