如何将dbgrid的纪录根据字段的特定值显示不同的颜色?

解决方案 »

  1.   

    //改变颜色。
    procedure TF_Query.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    var I : Integer;
    begin
      if Pos('未签收',F_Data.Query_Query.FieldByName('Flag').AsString)<>0 then
      begin
        DBGrid1.Canvas.Font.Color := clblue;//改变字体的颜色
        DBGrid1.DefaultDrawColumnCell(Rect, datacol,column, State);
      end;
      if F_Data.Query_Query.FieldByName('Printed').AsString='1' then
      begin
        DBGrid1.Canvas.Brush.Color := clSkyBlue;//改变背景的颜色
        DBGrid1.DefaultDrawColumnCell(Rect, datacol,column, State);
      {end
      else
      begin
        DBGrid1.Canvas.Brush.Color := clInfoBk;
        DBGrid1.DefaultDrawColumnCell(Rect, datacol,column, State);}
      end;
      if CheckBox1.Checked then
      begin
        if Print_Select.Count<>0 then
        begin
          for I:=0 to Print_Select.Count-1 do
          begin
            if Print_Select.Strings[I]=F_Data.Query_Query.FieldByName('Flow_ID').AsString then
            begin
              DBGrid1.Canvas.Brush.Color := clGray;
              DBGrid1.DefaultDrawColumnCell(Rect, datacol,column, State);
              Exit;
            end;
          end;
        end;
      end;
    end;
      

  2.   

    Delphi中数据控制构件DBGrid是用来反映数据表的最重要、也是最常用的构件。在应用程序中,如果以彩色的方式来显示DBGrid,将会增加其可视性,尤其在显示一些重要的或者是需要警示的数据时,可以改变这些数据所在的行或列的前景和背景的颜色。  DBGrid属性DefaultDrawing是用来控制Cell(网格)的绘制。若DefaultDrawing的缺省设置为True,意思是Delphi使用DBGrid的缺省绘制方法来制作网格和其中所包含的数据,数据是按与特定列相连接的Tfield构件的DisplayFormat或EditFormat特性来绘制的;若将DBGrid的DefaultDrawing特性设置成False,Delphi就不绘制网格或其内容,必须自行在TDBGrid的OnDrawDataCell事件中提供自己的绘制例程(自画功能)。  DBGrid的一个重要属性:画布Canvas,很多构件都有这一属性。Canvas代表了当前被显示DBGrid的表面,你如果把另行定义的显示内容和风格指定给DBGrid对象的Canvas,DBGrid对象会把Canvas属性值在屏幕上显示出来。具体应用时,涉及到Canvas的Brush属性和FillRect方法及TextOut方法。Brush属性规定了DBGrid.Canvas显示的图像、颜色、风格以及访问Windows GDI 对象句柄,FillRect方法使用当前Brush属性填充矩形区域,方法TextOut输出Canvas的文本内容
      

  3.   

    我来说个简单一点的
    打开DBGrid字段管理器-》选中要改变颜色的某个字段-》属性栏-》color改变一下,哈哈,ok搞定!
      

  4.   

    同意 daidai0(呆呆0) ,嘿嘿。历害历害,PFPF
      

  5.   

    不对不对!题目是“根据字段的特定值”!而 daidai0(呆呆0) 同志是所有的都变颜色了。
      

  6.   

    赫赫!    pukerno3(飞天)  100分   有没有意见
      

  7.   

    烦啊
    自己画吧
    用ehlib中的dbgrideh好些
      

  8.   

    procedure DBGridColumnColor(Sender: TObject;const Rect: TRect;
              DataCol: Integer; Column: TColumn;State: TGridDrawState;
              singlecolor:TColor;doublecolor:TColor;selectedcolor:TColor);
    var
      dataset:tdataset;
      dbgrid:tdbgrid;
    begin
      dataset:=TDBGrid(Sender).DataSource.DataSet;
      dbgrid:=TDBGrid(Sender);
      if (dataset.Recno mod 2) <> 0 then  //隔行
         begin
           dbgrid.Canvas.Font.Color := clBlack;
           dbgrid.Canvas.Brush.Color := singlecolor;  //这里设置单元格填充色
           dbgrid.Canvas.FillRect(Rect);  //这里对相应的单元格进行背景色填充
        end
      else
        begin
          dbgrid.Canvas.Font.Color := clBlack;
          dbgrid.Canvas.Brush.Color :=doublecolor;
          dbgrid.Canvas.FillRect(Rect);
        end;  //输出内容
      dbgrid.Canvas.TextRect( Rect, Rect.Left, Rect.Top, Column.Field.DisplayText);
      if gdSelected in State then  //被选中行的颜色
      begin
        dbgrid.Canvas.Font.Color := clBlack;
        dbgrid.Canvas.Brush.Color :=selectedcolor;
        dbgrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      end;
    end;