![]() |
#2
yuutian2011-07-11 17:43
我给出以下的三种,你可以参考以下:
第1种 如果你想要为TDBGrid中所选的某一格或某些格指定颜色,而且你不想使用dgRowSelect选项,因为你想让TDBGrid可以直接在TDBGrid单元格中编辑数据,你应该使用TDBGrid的OnDrawColumnCell事件。 下面我们用到的技巧可以动态改变TDBGrid中的单元格文本的颜色。 代码如下: procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if Table1.FieldByName(’Salary’).AsCurrency>36000 then //指定所需改变颜色行的条件表达式 DBGrid1.Canvas.Font.Color:=clMaroon; //指定颜色为clMaroon DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end; 上述代码执行的功能是:把薪水超过3万6千元的员工(employee)记录字体颜色用栗色(Maroon)标出来。 第2种 如何动态改变TDBGrid中行的颜色,代码如下: procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if Table1.FieldByName(’Salary’).AsCurrency>36000 then DBGrid1.Canvas.Brush.Color:=clWhite; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end; 上述代码执行的功能是:把薪水超过3万6千元的员工(employee)记录背景用白色(White)标出来 第3种 如何改变指定列中某些单元格的背景色,代码如下: 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; 上述代码执行的功能是:把薪水超过4万的员工(employee)记录背景用黑色(White)标出来而文本用白色标出来。 |
我用dbgrid显示了很多数据,对于其中的一列,我想依据此列显示的数值不同,<100的显示红色,,>=100的依旧显示黑色,可是我尝试了好多次,都全部显示为红色,我该怎么做?附我的代码:DELPHI7
begin
if DBGrid1.Fields[9].Value<100 then
begin
DBGrid1.Columns.Items[9].Font.color:=clRed;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;