各位大虾:
    小弟在编写程序的时候,遇到要在DBGRID中,根据不同的行里面的数据的不同,该行显示出不同的颜色?不知道用什么方法可行,请各位多多指教,帮帮小弟。不胜感激阿!!

解决方案 »

  1.   

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumnEh;
      State: TGridDrawState);
    begin
     if not (gdFixed in State) then
      begin
        with DBGrid1.Canvas do
        begin
        if DataModule1.Query1.fieldbyname('余额')>0 then
                 Brush.Color := clGreen
          else
            Brush.Color:=clred;
          FillRect(Rect);
          Font.Color:=clblack;
          TextOut(Rect.Left, Rect.Top, Field.AsString);
        end;
      end;  
    end;end.
      

  2.   

    我写了一个ColorDBGrid控件, 想要的话留下Email.
      

  3.   

    ---- 在Delphi文件菜单中选择New建立新的应用程序工程Project1和主窗体Form1,设置Form1的Caption属性为“控制DBGrid行颜色的示例”。在主窗体上添加Data Source、Table、Button和ColoredDBGrid组件。设置各组件的属性如下: Table1.Database=’DBDemos’
    Table1.Tablename=’example.db’
    Datasource1.Dataset=Table1
    ColoredDBGrid1.Datasource=DataSource1
    Button1.Caption=’退出’---- 在ColoredDBGrid1的onDRawColoredDBGrid事件中输入下列代码,设定由Wage(工资)来决定在ColoredDBGrid1各行的颜色。 
    procedure TForm1.ColoredDBGrid1 DRawColoredDBGrid 
    (Sender: TObject;  Field: TField; var Color: 
    TColor; var Font: TFont);
    Var
      p : Integer;
    begin
        p := Table1.FindField('wage').AsInteger;
      //取得当前记录的Wage字段的值。
        if(p < 500) then begin                 
    //程序将根据wage值设置各行的颜色。
          Color := clGreen;
          Font.Style := [fsItalic];      
    //不仅可以改变颜色,还可以改变字体
        end;
        if(p >= 500) And (p < 800) then
          Color := clRed;
         if(p >=800) then begin
          Color := clMaroon;
          Font.Style := [fsBold];
        end;
    end;
    //用‘退出’按钮结束程序运行。
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    Close;
    end;
    得到预计的结果:第一行和第三行变为红色,第二行变为棕色,第四行为绿色,满足了基本要求。 
      

  4.   

    首先将DBgrid的DefaultDrawing属性设置为false;  
    然后在DBBrid的DrawColumnCell事件中这样做:  
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
      DataCol: Integer; Column: TColumn; State: TGridDrawState); 
    begin 
      if (Table1.Recno mod 2) <> 0 then begin 
        DBGrid1.Canvas.Font.Color := clWhite; 
        DBGrid1.Canvas.Brush.Color := clRed;  //这里设置单元格填充色 
        DBGrid1.Canvas.FillRect(Rect);        //这里对单元格进行背景色填充 
      end; 
      //输出内容 
      DBGrid1.Canvas.TextRect( Rect, Rect.Left, Rect.Top, Column.Field.AsString); 
    end;
      

  5.   

    修改DBGRID的FixedColor属性、WFistColor属性及WSecondColor属性。
    在窗体的创建事件中加上代码:
    DBGRID.WZebra := true;