当鼠标点第二行的某一格,整个第二行的颜色变成黄色,用来突出选的那格是第二行,当选第三或其它行,所选行变成黄色,第二行的颜色就变回原来的颜色,如何
编程,请写出程序

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, DBTables, Grids, DBGrids;type
      TMainForm = class(TForm)
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        Table1: TTable;
        procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
      if gdSelected in State then
      begin
        DBGrid1.Canvas.Brush.Color := clYellow ;
        DBGrid1.Canvas.Font.Color := clRed;
      end
      else
        DBGrid1.Canvas.Brush.Color := clWhite;  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);      
    end;end.
      

  2.   

    同意楼上,但是别忘了要把DBGrid的Option中的dgRowSelect属性设为True,否则的话只有被选择的单元变色,而不是整行.
      

  3.   

    设置options的dgRowSelect属性设为True
    还有optionsEh的dghRowHighlight属性设为True
      

  4.   

    可不可以用编程方法,不用属性,把Option中的dgRowSelect属性设为True后
    我做的另一效果又不行
      

  5.   

    我想知道DBGrid怎样改变行的颜色
      

  6.   

    用DBGrid1只能这样,
    用StringGrid试试。
      

  7.   

    happy1123(飞天神鼠) ,在RX控件集中,哪儿可以找得到例程啊?我找了半天都没找到
      

  8.   

    哈哈哈哈,到了没想到你的问题还没有解决呀?这样吧,如果你能耐心看完下面这一段,问题应该就解决了。有个问题是在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、程序运行结果 
    得到预计的结果:第一行和第三行变为红色,第二行变为棕色,第四行为绿色,满足了基本要求。
      

  9.   

    不是这个意思,我只需要一种颜色,而且不需要条件判断(if(p >=800) then),只要选择的行的颜色与其它行的颜色不同就行
      

  10.   

    在DBGrid1DrawColumnCell事件中增加   
          with TCustomDBGridCracker(sender) do begin
               begin
                if (DataSource.DataSet.RecNo )=1 then
                  canvas.brush.color:=clblue;
                  canvas.Font.Style:=[];
                  canvas.Font.Size :=9;
                  canvas.Font.Color :=clblack;
               end;
            defaultdrawcolumncell(Rect,DataCol,Column,State);
      

  11.   

    你是不是下的版本不对,csdn网站有下的
      

  12.   

    我在WWW.playicq.com下的。我再下下看。谢谢了
      

  13.   

    http://www.csdn.net/cnshare/soft/4/4575.shtm此地址有下的。我的是买光盘带的