如何让dbgrideh的某一个单元格根据条件显示特定颜色,GetCellParams好像只能让整行或者整列变色

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4537/4537841.xml?temp=.9666712
      

  2.   

    以下仅做参考:
    procedure TFormAutoCal.StringGrid1DrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    begin
      if trim(StringGrid1.Cells[4,ARow]) = '废标' then
      with stringgrid1 do
      begin
        canvas.Brush.color:= clRed;
        canvas.Font.Color := clWhite;
        canvas.FillRect(Rect);
        canvas.TextOut(rect.left+2,rect.top+2,cells[acol,arow])
      end;
        if trim(StringGrid1.Cells[4,ARow]) = '未投标' then
      with stringgrid1 do
      begin
        canvas.Brush.color:=clblue;
        canvas.Font.Color := clWhite;
        canvas.FillRect(Rect);
        canvas.TextOut(rect.left+2,rect.top+2,cells[acol,arow])
      end;
    end;
      

  3.   

    不好意思,我看错了,这个是StringGrid的。
      

  4.   

    可以参考一下:procedure TFormMain.DBGridActDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      valX, valY :Integer;
      valStr :String;
    begin
      with TDBGrid(Sender) do
      begin
       if ((State = [gdSelected]) or (State=[gdSelected,gdFocused]))  then
        begin
         Canvas.Font.Color :=clWhite;
         Canvas.Brush.Color :=$00C08080;
        end
        else
        begin
          if DataModuleSQL.ADOQuerySQL.RecNo mod 2<>0 then
          begin
            Canvas.Brush.Color :=clWhite
          end
          else
          begin
            Canvas.Brush.Color :=$00CFFEFD;
          end;
        end;    DefaultDrawColumnCell(Rect, DataCol, Column, State);    case DataCol of
          1:
          begin
            case (DataModuleSQL.ADOQuerySQL.FieldByName('频道').AsInteger) of
              1:
              begin
                valStr := '频道 1';
              end;
              2:
              begin
                valStr := '频道 2';
              end;
            end;        valX := ((Rect.Left + Rect.Right)
                    - Canvas.TextWidth(valStr)) div 2;
            valY := ((Rect.Top + Rect.Bottom)
                    - Canvas.TextHeight(valStr)) div 2;
    Canvas.Brush.Color :=clRed;
            Canvas.TextOut(valX, valY, valStr);
          end;
          ......
        end;
      end;
    end;
      

  5.   

    还是不行,只能设定特定行或者列的颜色,如果要设定特定单元格的颜色呢?例如
    时间    是否预计
    12:00      true
    13:00      false
    根据是否预计字段来确定时间字段的字体颜色
      

  6.   

    procedure TForm1.DBGridEh1GetCellParams(Sender: TObject; Column: TColumnEh;
      AFont: TFont; var Background: TColor; State: TGridDrawState);
    begin
      if column.FieldName = '时间' then
      if dbgrideh1.datasource.dataset.FieldByName('是否预计').asstring='true' then
        AFont.Color:= clRed
      else
        AFont.Color:= clBlue;
    end;