我想判断三列值的比较判断条件后的背景颜色变化,如下,不知哪里有错误?另外,还要注意什么才能演示成功?我是菜鸟所以,请详细点,谢谢!
procedure TForm17.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if ADOQuery1.FieldByName('数据').AsString < ADOQuery1.FieldByName('最小值').AsString then
DBGrid1.Canvas.Brush.Color:=clred;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
if ADOQuery1.FieldByName('数据').AsString > ADOQuery1.FieldByName('最大值').AsString then
DBGrid1.Canvas.Brush.Color:=clred;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

解决方案 »

  1.   


    当“字段2”小于“字段1”值时,该行显示红色。当“字段2”大于“字段3”值时,该行显示黄色。
    三个字段都是ADOQuery1的查询值。
    请教代码如何,详细一点更为感谢!
      

  2.   

    procedure TForm7.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      with DBGrid1.DataSource.DataSet do begin
        if FieldByName('字段2').AsInteger < FieldByName('字段1').AsInteger then
          DBGrid1.Canvas.Brush.Color:=clred
        else if FieldByName('字段2').AsInteger > FieldByName('字段3').AsInteger then
          DBGrid1.Canvas.Brush.Color:=clYellow
        else
          DBGrid1.Canvas.Brush.Color:=clWindow;
      end;
      DBGrid1.Canvas.FillRect(Rect);
      if (State=[gdSelected])or(State=[gdSelected,gdFocused]) then
        DBGrid1.Canvas.font.color:=clBlue
      else
        DBGrid1.Canvas.font.color:=clblack;
      DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;
      

  3.   

    当“字段2”小于“字段1”值时,该行显示红色。当“字段2”大于“字段3”值时,该行显示黄色。
    “字段1”“字段2”“字段3”这三个字段都是ADOQuery1的得出的计算值。楼上能否改下,用ADOQuery。
    请教代码如何,详细一点更为感谢!
      

  4.   

    你的代码中,一共有两个“DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);”,去掉前一个,只需要在最后用这一句刷新显示就可以了。
      

  5.   

    我的是WIN7系统,不知无法变色和这个系统有没有关系。
      

  6.   


    上面这个不行??还有将AsString改成AsInteger。因为用AsString判断结果不一定正确。
      

  7.   

    奇怪,不行。另外,我在该论坛搜索的几个例子,都无不能显示颜色变化。
    我的是win7 64 操作系统,xe2软件。难道和这个有关。
    procedure TForm17.DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
    if ADOQuery2.FieldByName('入库数量').AsInteger = 1 then
     begin
      dbgrid2.Canvas.Brush.Color:=clYellow;
      DBGrid2.DefaultDrawColumnCell(Rect,DataCol,Column,State);
     end;
    end;
    颜色没反应。
    procedure TForm17.DBGrid4DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      with DBGrid4.DataSource.DataSet do begin
        if FieldByName('库存').AsInteger < FieldByName('安全库存').AsInteger then
          DBGrid4.Canvas.Brush.Color:=clred
        else if FieldByName('库存').AsInteger > FieldByName('最高库存').AsInteger then
          DBGrid4.Canvas.Brush.Color:=clYellow
        else
          DBGrid4.Canvas.Brush.Color:=clWindow;
      end;
      DBGrid4.Canvas.FillRect(Rect);
      if (State=[gdSelected])or(State=[gdSelected,gdFocused]) then
        DBGrid4.Canvas.font.color:=clBlue
      else
        DBGrid4.Canvas.font.color:=clblack;
      DBGrid4.DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;
    颜色也没反应。
    到底什么原因?难道没有一位能够找到问题的原因?
      

  8.   


    我在Win2008 R2 64位下面用Delphi2010用第一個方法沒問題的。XE2沒試過了。
      

  9.   

    你表格连接的数据集是ADOQuery1吧?
    语句with DBGrid1.DataSource.DataSet do begin这里使得指向了连接的数据集,无论你用的是什么名字。为什么还要改呢?我的代码是在windows_xp + delphi7及2010 测试过才回你帖子的。
      

  10.   

    我的是win7 64 操作系统,xe2软件。难道和这个有关。和这个有关!
    部分系统颜色是会变的,不能显示,有限颜色好像可以显示,红色?