procedure Tform_outplate.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
    with DBGrid1.Canvas do begin
    if Column.Title.Caption = '出电' then
      Brush.Color := clRed
    else if Column.Title.Caption = '返电' then
      Brush.Color := clBlue
    else if Column.Title.Caption = '改电' then
      Brush.Color := clWhite;
    FillRect(Rect);
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;
end;我只想改变字体的颜色,不想改变整个cell的颜色,请问怎么做?

解决方案 »

  1.   

    你用了FillRect(Rect)是画cell背景的,改字体要用Font,
    要改成这样:
        with DBGrid1.Canvas do begin
        if Column.Title.Caption = '出电' then
          Font.Color := clRed
        else if Column.Title.Caption = '返电' then
          Font.Color := clBlue
        else if Column.Title.Caption = '改电' then
          Font.Color := clGreen;
        DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
        end;
      

  2.   

    你要确认你的DBGrid的Column.Title.Caption是否符合条件啊。
      

  3.   

    应该写在DrawDatacell事件中
    procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    begin
     if field.FieldName='Traffic_No' then
       DBGrid1.Canvas.font.Color:=clRed;
    // FillRect(Rect);
     DBGrid1.DefaultDrawDataCell(Rect, Field,State);
    end;
      

  4.   

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if column.FieldName='NAME' then
      begin
        if (dbgrid1.DataSource.DataSet.RecNo mod 2)=0 then
          DbGrid1.Canvas.Brush.Color:= clAqua;
      end;
      If ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
        DbGrid1.Canvas.Brush.Color:= clred;  DbGrid1.DefaultDrawColumnCell (Rect,DataCol,Column,State);
    end;
      

  5.   

    把属性defaultdrawing改为false试试看
      

  6.   

    DefaultDrawColumnCell是不是又重置了颜色,你可以参看一下VCL的代码,看原来是怎么实现的
      

  7.   

    with DBGrid1.Canvas do begin
        if Column.Title.Caption = '出电' then
          Canvas.Font.Color := clRed
        else if Column.Title.Caption = '返电' then
          Canvas.Font.Color := clBlue
        else if Column.Title.Caption = '改电' then
          Canvas.Font.Color := clGreen;
        DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
        end;