先搜索一下好吗………………
在OnDrawColumnCell中处理

解决方案 »

  1.   

    //==============================================================================
    //根据状态设置Cell颜色******************************************************
    //==============================================================================
    procedure TForm_BillManage.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if Column.Field.FieldName='状态'
      then begin                            
             //=====================================================================
             if Column.Field.AsString='正常'
             then begin
                    DBGrid.Canvas.Brush.Color := DBGrid.Color;
                    DBGrid.Canvas.Font.Color := clBlack;
                    //DBGrid.Canvas.Brush.Color := States[0].CellColor;
                    //DBGrid.Canvas.Font.Color := States[0].FontColor;
                  end;
             //=====================================================================
             if Column.Field.AsString='丢失'
             then begin
                    DBGrid.Canvas.Brush.Color := States[3].CellColor;
                    DBGrid.Canvas.Font.Color := States[3].FontColor;
                  end;
             //=====================================================================
             if Column.Field.AsString='作废'
             then begin
                    DBGrid.Canvas.Brush.Color := States[6].CellColor;
                    DBGrid.Canvas.Font.Color := States[6].FontColor;
                  end;
           end;
      //============================================================================
      DBGrid.DefaultDrawDataCell(Rect, Column.Field, State);
    end;
      

  2.   

    好例子
    unit MainFrm;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Db, DBTables, Grids, DBGrids, ComCtrls, StdCtrls, ExtCtrls;type
      TMainForm = class(TForm)
        dbgMain: TDBGrid;
        Query2: TQuery;
        DataSource1: TDataSource;
        dbgSecond: TDBGrid;
        Label4: TLabel;
        Label5: TLabel;
        procedure dbgMainDrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
        procedure dbgSecondDrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.dbgMainDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
     if gdSelected in State then 
        Exit;
      if Query2.RecNo mod 2 = 0 then
        dbgMain.Canvas.Brush.Color := clInfoBk
      else
        dbgMain.Canvas.Brush.Color := RGB(191, 255, 223);  dbgMain.DefaultDrawColumnCell(Rect,DataCol,Column,State);
      dbgMain.Canvas.Pen.Color := $00C08000;
      dbgMain.Canvas.MoveTo(Rect.Left, Rect.Bottom);
      dbgMain.Canvas.LineTo(Rect.Right, Rect.Bottom);
      dbgMain.Canvas.MoveTo(Rect.Right, Rect.Top);
      dbgMain.Canvas.LineTo(Rect.Right, Rect.Bottom);
    end;procedure TMainForm.dbgSecondDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin  
      with dbgSecond.Canvas do
      begin
        Pen.Color := clBlue;
        MoveTo(Rect.Left, Rect.Bottom);
        LineTo(Rect.Right, Rect.Bottom);
        Pen.Color := clGreen;
        MoveTo(Rect.Right, Rect.Top);
        LineTo(Rect.Right, Rect.Bottom);
      end;
    end;end.