Delphi DBGrid高亮显示选中行源码


Delphi DBGrid高亮显示选中行源码
你的代码不变,添加红色部分代码即可。

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, DB, ADODB, Menus;

type
TCustomDBGridCracker = class(TCustomDBGrid);  
 TForm1 = class(TForm)
   procedure Button1Click(Sender: TObject);
   procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
     DataCol: Integer; Column: TColumn; State: TGridDrawState);
   procedure FormCreate(Sender: TObject);
   procedure Button2Click(Sender: TObject);
   procedure N1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation
{$R *.dfm}

进入DBGrid events里双击DBGrid1DrawColumnCell,添加红色代码

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
 DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with TCustomDBGridCracker(sender) do
 begin
   if DataLink.ActiveRecord = Row - 1 then // 如果被选中
   begin
     //Canvas.Brush.Color := clRed; //选中时颜色
     //canvas.Font.Color := clyellow; //选中的字体颜色
     canvas.Font.Style := [fsBold]; //选中时的字体
     //canvas.Font.Size := 10;
   end
   else
   begin
         {if (DataSource.DataSet.RecNo mod 2)=0 then
         canvas.brush.color:=clgreen  else }
     canvas.brush.color := clwindow;
     canvas.Font.Style := [];
     //canvas.Font.Size := 9;
     //canvas.Font.Color := clblack;
   end;
   defaultdrawcolumncell(Rect, DataCol, Column, State);
 end;

end;

end.

转至:http://www.object8888.com/article.asp?id=177

你可能感兴趣的:(Delphi,dbgrid,高亮显示选中行源码)