如何设置DBGrid中某一单元格中数据的颜色,如:设置Cell(3,6)单元格的颜色为红色

解决方案 »

  1.   

    设置过某行,没有单独设置一个格子颜色
    用下面这个改造一下,试试
    procedure TdfrmSt_Decl.DBGrid2DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
      inherited;
      IF column.FieldName='IfPur' then
      begin
        IF column.Field.AsBoolean=true then
           begin
      DBGrid2.Canvas.Font.Height:=12;
      DBGrid2.Canvas.TextRect(Rect,Rect.left+1,Rect.top+1,'');
           end
         Else
           begin 
      DBGrid2.Canvas.Font.Height:=12;
      DBGrid2.Canvas.TextRect(Rect,Rect.left+1,Rect.top+1,'·ñ');
           end;
      end;
    end;    IF Column.Field.AsBoolean then
        begin
          DBGrid2.Canvas.Font.Color:=clGreen;
          DBGrid2.Canvas.Font.Height:=12;
          DBGrid2.Canvas.Font.Style:=[fsBold];
          DBGrid2.Canvas.TextRect(Rect,Rect.Left+1,Rect.Top+1,'   ¡Ì')
      

  2.   

    If you want to color the selected row in a DBGrid but you don't
    want to use the dgRowSelect option because you want to be able
    to edit the data you can use the following technique on the 
    DBGrid.OnDrawColumnCell event:type
      TCustomDBGridCracker = class(TCustomDBGrid);
     
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; 
      const Rect: TRect; DataCol: Integer; Column: TColumn; 
      State: TGridDrawState);
    begin
      with Cracker(Sender) do
        if DataLink.ActiveRecord = Row - 1 then
          Canvas.Brush.Color := clRed
        else
          Canvas.Brush.Color := clWhite;
      DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;
      

  3.   

    我有一个现成的函数,感觉挺好用,楼主可以试试看,最好把它写在DLL中,以后每个工程都可以调用的(*******************************************************************************
    *   过程名:SetGridColor
    *   过程说明:  用于设置数据库应用程序的DBGrid的数据显示时的颜色
    *               可在TDBGrid的DrawColumnCell事件中进行设置,例如:
    *               SetGridColor(TDataSet(CompanyADTable),DBGrid1,Rect,DataCol,Column,State);*   参数说明:
    *   AdoTable:数据库对象名称(可重载,可以使用ADOTABLE,ADOQUERY,TTABLE,TQUERY对象.全部从TDataSet中继承得来)
    *   DbGrid1:要显示的数据库TDBGRID对象
    *   Rect,DataCol,Column,State:这四个参数可全部从TDBGrid的DrawColumnCell事件中传递过来!
    *******************************************************************************)Procedure SetGridColor(Var AdoTable:TDataSet;Var DbGrid1:TDBGrid;const Rect: TRect; DataCol: Integer;
     Column: TColumn; State: TGridDrawState);
    begin
      Case AdoTable.RecNo mod 2 = 0 of // file://根据数据集的记录号进行判断
        True: DbGrid1.Canvas.Brush.color:=clcream;// file://偶数行用浅绿色显示
        False: DbGrid1.Canvas.Brush.color:= clwhite;// file://奇数行用蓝色表示
      end;
      If ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
        Case DataCol mod 2 = 0 of
        True :
        begin
           DbGrid1.Canvas.Brush.color:=$00ffff; //file://当前选中行的偶数列用红色
           dbgrid1.Canvas.Font.Color:=clred;
        end;
        False:
        begin
          DbGrid1.Canvas.Brush.color:=$00ffff;// file://当前选中行的奇数列用绿色表示
          dbgrid1.Canvas.Font.Color:=clred;
        end;
        end;
      DbGrid1.Canvas.pen.mode:=pmMask;
      DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
    end;