delphi7中设置dbgird的列 评委1、2、3、4、5 列 如果 其中某个值 以同行的 平均分 列值 相差+-10 以外则单元格的字体或是背景 变成黄色,如第一行中评委1列值90 与平均分79.2相差超过了10 所以它以黄色显示 ,请指教,最好请给出代码(通过adoquery1来查询里面的数据)单位名称 平均分 评委 1 评委 2 评委 3 评委 4 评委 5
单位1 79.2 90 70 76 80 80
单位2 32.8 34 44 77 4 5
单位3 55.2 45 44 66 56 65
单位4 47.6 45 44 66 78 5
单位1 79.2 90 70 76 80 80
单位2 32.8 34 44 77 4 5
单位3 55.2 45 44 66 56 65
单位4 47.6 45 44 66 78 5
begin
if Table1.FieldByName(’field1’).AsCurrency-Table1.FieldByName(’avg’).AsCurrency>10 then
//指定所需改变颜色行的条件表达式
DBGrid1.Canvas.Font.Color:=clYellow;
//指定颜色为clMaroon
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
if Abs(Table1.FieldByName(’field1’).AsCurrency-Table1.FieldByName(’avg’).AsCurrency)>10 then
if Table1.FieldByName(’field1’).AsCurrency-Table1.FieldByName(’avg’).AsCurrency>10 then
这个if很显然没判断列,其实参数中有DataCol和Column表示当前画的单元格是哪一列,把列的条件也给加上
例子代码是个思路,看懂了自己写代码。Column,DataCol变量都可以使用。
Column.FieldName 标记了当前刷新的是哪个fieldif Column.FieldName='评委 1 ' then
if Table1.FieldByName(’field1’).AsCurrency-Table1.FieldByName(’avg’).AsCurrency>10 then
..................
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (Pos('评委', Column.FieldName) = 1) and//判断是不是评委那个字段对应的列
(Abs((Column.Field.AsFloat - ADOQuery1.FieldByName('平均分').AsFloat)) > 10) then
begin
//指定所需改变颜色行的条件表达式
DBGrid1.Canvas.Font.Color := clYellow;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;