delphi中 DBGrid显示数据库内容,使用MouseCoord(X,Y)获取鼠标指向的是第几条记录
答案:1 悬赏:60 手机版
解决时间 2021-11-14 17:51
- 提问者网友:相思似海深
- 2021-11-14 08:34
delphi中 DBGrid显示数据库内容,使用MouseCoord(X,Y)获取鼠标指向的是第几条记录
最佳答案
- 五星知识达人网友:鸠书
- 2021-11-14 09:18
比较麻烦,目前想到一个笨办法。
{
显示DBGrid中鼠标指向的记录号
by 宋银海
}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private declarations }
FDBRow:Integer;
FDBCurrentRec:Integer;
public
{ Public declarations }
published
property DBRow: Integer read FDBRow write FDBRow;
property DBCurrentRec: Integer read FDBCurrentRec write FDBCurrentRec;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Table1.Active then
Table1.Open;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
DBRow:=(Rect.Bottom-DBGrid1.Top+1) div (Rect.Bottom-Rect.Top+1);
DBCurrentRec:=DBGrid1.DataSource.DataSet.RecNo;
end;
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
pt:TGridCoord;
begin
pt:=(Sender as TDBGrid).MouseCoord(x,y); //获取DBGrid中的坐标
Caption:='鼠标指向记录号是 '+IntTostr(DBCurrentRec+pt.Y-DBRow);
end;
end.
{
显示DBGrid中鼠标指向的记录号
by 宋银海
}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private declarations }
FDBRow:Integer;
FDBCurrentRec:Integer;
public
{ Public declarations }
published
property DBRow: Integer read FDBRow write FDBRow;
property DBCurrentRec: Integer read FDBCurrentRec write FDBCurrentRec;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Table1.Active then
Table1.Open;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
DBRow:=(Rect.Bottom-DBGrid1.Top+1) div (Rect.Bottom-Rect.Top+1);
DBCurrentRec:=DBGrid1.DataSource.DataSet.RecNo;
end;
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
pt:TGridCoord;
begin
pt:=(Sender as TDBGrid).MouseCoord(x,y); //获取DBGrid中的坐标
Caption:='鼠标指向记录号是 '+IntTostr(DBCurrentRec+pt.Y-DBRow);
end;
end.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯