怎樣給dbgrid某個單元格指定一種顏色﹖謝謝﹗

解决方案 »

  1.   

    var
      ch:string;
    begin
      ch:=Field.fieldname;
    if ch='你好' then DBGrid1.canvas.brush.color:=clRed;
    end
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Db, Grids, DBGrids, DBTables;type
      TForm1 = class(TForm)
        Table1: TTable;
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure FormCreate(Sender: TObject);
        procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Action:=CaFree;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Table1.DatabaseName:='test';
      Table1.TableName:='test.db';
      Table1.Active:=True;
      Datasource1.DataSet:=Table1;
      DBGrid1.DataSource:=DataSource1;
      DBGrid1.Align:=alClient;
    end;procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      Case Datacol Mod 2=0 of
      True:DBGrid1.Canvas.Brush.Color:=CLRed;
      False:DBGrid1.Canvas.Brush.Color:=CLGreen;
      end;
      if ((State=[GdSelected]) or (State=[GdSelected,GdFocused])) then
      DBGrid1.Canvas.Brush.Color:=CLBlue;
      DBGrid1.DefaultDrawColumnCell(Rect,Datacol,Column,State);
    end;end.