比如选择行的颜色
或者第几条记录的颜色谢谢帮忙  在线等待

解决方案 »

  1.   

    procedure TfmLcarArrange.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
    if (Rect.Top = TStringGrid(DBGrid1).CellRect(TStringGrid(DBGrid1).col,TStringGrid(DBGrid1).row).top)
     and(not (gdFocused in State)) then
     Dbgrid1.Canvas.Brush.Color := clmoneygreen;
     DBGrid1.DefaultDrawDataCell(Rect, Column.Field, State);
    end;另外精华区里有类似的题目
      

  2.   

    在DBGrid的DBGrid1DrawColumnCell事件中写代码:
      try
        if 条件 then
             DBGrid1.Canvas.Font.Color := clBlue;     
      except
      end;
      

  3.   

    procedure TSellerQueryForm.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
      If gdSelected In State then
       begin
         DBGrid1.Canvas.Brush.Color := clMoneyGreen;  
         DBGrid1.Canvas.Font.Color := clRed;
      end
      else
        DBGrid1.Canvas.Brush.Color := clWhite;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;
      

  4.   

    Delphi中的数据网格控件(TDbGrid)对于显示和编辑数据库中大量的数据起着十分重要的作用;然而,在使用数据网格控件的同时,也往往因为表格中大量的数据不易区分,而令操作者眼花缭乱。如何提高网格控件的易用性,克服它的此项不足呢?本文从改变数据网格的色彩配置角度,提出了一种解决办法。
      以下为数据网格控件的6种特殊效果的实现方法,至于数据网格控件与数据集如何连接的方法从略。
    1. 纵向斑马线效果:实现网格的奇数列和偶数列分别以不同的颜色显示,以区别相邻的数据列。
      file://在DbGrid的DrawColumnCell事件中编写如下代码:
      Case DataCol Mod 2 = 0 of
       True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶数列用蓝色
       False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇数列用浅绿色
      End;
      DbGrid1.Canvas.Pen.Mode:=pmMask;
      DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
    2. 纵向斑马线,同时以红色突出显示当前单元格效果:以突出显示当前选中的字段。
      file://将上述代码修改为:
      Case DataCol Mod 2 = 0 of
       True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶数列用蓝色
       False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇数列用浅绿色
      End;
      If ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
        If Not DbGrid1.SelectedRows.CurrentRowSelected then
          DbGrid1.Canvas.Brush.Color:=clRed; file://当前选中单元格显示红色
          DbGrid1.Canvas.Pen.Mode:=pmMask;
          DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
    上述两种方法突出了列的显示效果。
    3.在数据网格中以红色突出显示当前选中的行:
      设置DbGrid控件的Options属性中的dgRowSelect属性为真,Color属性为clAqua(背景色), 在DbGrid的DrawColumnCell事件中编写如下代码:
      if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
       DbGrid1.Canvas.Brush.color:=clRed; file://当前行以红色显示,其它行使用背景的浅绿色
       DbGrid1.Canvas.pen.mode:=pmmask;
       DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
    4.行突显的斑马线效果:既突出当前行,又区分不同的列(字段)。
      file://其它属性设置同3,将上述代码修改为:
      if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
       begin
        Case DataCol Mod 2 = 0 of
         True : DbGrid1.Canvas.Brush.color:=clRed; file://当前选中行的偶数列显示红色
         False: DbGrid1.Canvas.Brush.color:=clblue; file://当前选中行的奇数列显示蓝色
        end;
       DbGrid1.Canvas.pen.mode:=pmmask;
       DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
      end;
    5.横向斑马线,同时以红色突显当前行效果:
      file://其它属性设置同3,将上述代码修改为:
      Case Table1.RecNo mod 2 = 0 of file://根据数据集的记录号进行判断
       True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶数行用浅绿色显示
       False: DbGrid1.Canvas.Brush.color:=clblue; file://奇数行用蓝色表示
      end;
      if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then file://选中行用红色显示
       DbGrid1.Canvas.Brush.color:=clRed;
       DbGrid1.Canvas.pen.mode:=pmMask;
       DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
    6.双向斑马线效果:即行间用不同色区分,同时,选中行以纵向斑马线效果区分不同的列。
      file://其它属性设置同3,将上述代码修改为:
      Case Table1.RecNo mod 2 = 0 of file://根据数据集的记录号进行判断
       True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶数行用浅绿色显示
       False: DbGrid1.Canvas.Brush.color:= clblue; file://奇数行用蓝色表示
      end;
      If ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
      Case DataCol mod 2 = 0 of
       True : DbGrid1.Canvas.Brush.color:=clRed; file://当前选中行的偶数列用红色
       False: DbGrid1.Canvas.Brush.color:= clGreen; file://当前选中行的奇数列用绿色表示
      end;
    DbGrid1.Canvas.pen.mode:=pmMask;
      DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
    7. 随意控制DBGrid 每一行的颜色:
    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;
      

  5.   

    再加一篇---- 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.ColoredDBGrid1DRawColoredDBGrid  (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 &lt; 800) then
        Color := clRed;
      if (p >= 800) then
      begin
        Color := clMaroon;
        Font.Style := [fsBold];
      end;
    end;//用‘退出’按钮结束程序运行。
    procedure TForm1.Button1Click(Sender: TObject);
    begin 
     Close;
    end;