在Delphi中随意控制DBGrid 每一行的颜色 
中国地质大学研究生院 
张兵兵 黄孝林 
---- 有个问题是在Delphi中使用DBGrid时,如何让DBGrid中每一行颜色按照用户自己的意愿控制。最初看到这个问题时,我们以为非常非常简单,所以马上动手准备解决它。结果却发现不是那么回事,传统方法根本不能发挥作用。在电脑面前一直坐到凌晨4点,不断地调试,幸运地是凭借平时积累的一点编程经验,终于找到了开门的匙钥。现将它充公,供大家享用。 ---- 1、 数据表的建立 ---- 在Delphi的工具菜单中选择Database desktop,在数据库DBDemos下建立一个名为example.db的数据表。数据表的字段和内容如下:               
Name Age Wage
张山 25 500
王武 57 1060
李市 30 520
刘牛 28 390---- 2、创建基于TDBGrid的TColoredDBGrid组件 
---- 在Delphi组件菜单中,选择New Component,在弹出对话框中作以下设置: Ancestor Type  =   TDBGrid
Class  Name   =   TColoredDBGrid---- 然后单击OK按钮,Delphi自动完成组件基本框架的定义。增添OnDRawColoredDBGrid事件并使它出现在Object Inspector的Events中以便在应用程序中设定改变行颜色的条件。重载DrawCell方法,只能自己绘制单元格。不能通过在OnDrawColumnCell来设置颜色,因为在OnDrawColumnCell改变单元格的颜色会再次触发OnDrawColumnCell。 
---- 下面就是所创建组件的源程序 。 ---- 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、程序运行结果 
---- 本程序在win98(中文版)和delphi4.0(C/S版)中测试运行,得到预计的结果:第一行和第三行变为红色,第二行变为棕色,第四行为绿色,满足了基本要求。    

解决方案 »

  1.   

    这样(我调试过,绝对没问题):
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
    if datamodule2.table2.recno mod 2=0 then
      {begin
        dbgrid1.canvas.Brush.color:=claqua;
        dbgrid1.DefaultDrawcolumnCell(rect,datacol,column,state);
      end}
    else
      begin
        dbgrid1.canvas.Brush.color:=clyellow;
        dbgrid1.DefaultDrawcolumnCell(rect,datacol,column,state);
      end;
    end;
      

  2.   

    不会如此复杂吧,还专门写一控件:
    在此给出较为详细的代码:
      以下用一个例子来详细地说明如何显示彩色的DBGrid。在例子中首先要有一个DBGrid构件,其次有一个用来产生彩色筛选条件的SpinEdit构件,另外还有ColorGrid构件供自由选择数据单元的前景和背景的颜色。  1.建立名为ColorDBGrid的Project,在其窗体Form1中依次放入所需构件,并设置属性为相应值,具体如下所列:
        Table1 DatabaseName: DBDEMOS
          TableName: EMPLOYEE.DB
          Active: True;
        DataSource1 DataSet: Table1
        DBGrid1 DataSource1: DataSource1
          DefaultDrawing: False
        SpinEdit1 Increment:200
                    Value: 20000
        ColorGrid1 GridOrdering: go16*1  2.为DBGrid1构件OnDrawDataCell事件编写响应程序:
      procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;Field: TField; State: TGridDrawState);
      begin
         if Table1.Fieldbyname(′Salary′).value<=SpinEdit1.value then
            DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor
       else
            DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor;
         DBGrid1.Canvas.FillRect(Rect);
         DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString);
      end;  这个过程的作用是当SpinEdit1给定的条件得以满足时,如′salary′变量低于或等于SpinEdit1.Value时,DBGrid1记录以ColorGrid1的前景颜色来显示,否则以ColorGrid1的背景颜色来显示。然后调用DBGrid的Canvas的填充过程FillRect和文本输出过程重新绘制DBGrid的画面。  3.为SpinEdit1构件的OnChange事件编写响应代码:
      procedure TForm1.SpinEdit1Change(Sender: TObject);
      begin
         DBGrid1.refresh;
      end;  当SpinEdit1构件的值有所改变时,重新刷新DBGrid1。  4.为ColorGrid1的OnChange事件编写响应代码:
      procedure TForm1.ColorGrid1Change(Sender: TObject);
      begin
         DBGrid1.refresh;
       end;  当ColorGrid1的值有所改变时,即鼠标的右键或左键单击ColorGrid1重新刷新DBGrid1。  5.为Form1窗体(主窗体)的OnCreate事件编写响应代码:
      procedure TForm1.FormCreate(Sender: TObject);
      begin
       ColorGrid1.ForeGroundIndex:=9;
       ColorGrid1.BackGroundIndex:=15;
      end;  在主窗创建时,将ColorGrid1的初值设定前景为灰色,背景为白色,也即DBGrid的字体颜色为灰色,背景颜色为白色。  6.现在,可以对ColorDBGrid程序进行编译和运行了。当用鼠标的左键或右键单击ColorGrid1时,DBGrid的字体和背景颜色将随之变化。
      

  3.   

    to :qywjg(重出江湖)ColoredDBGrid是啥东东?D5中处带的吗?不会吧