DBGrid中Shift多选
时间:2021-07-01 10:21:17
帮助过:22人阅读
procedure TFamEditPeople.DBGrid1KeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
FKeyShift := ssShift
in Shift;
end;
procedure TFamEditPeople.DBGrid1KeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
FKeyShift :=
False;
end;
procedure TFamEditPeople.DataSource1StateChange(Sender: TObject);
begin
FOldNo :=
FRecNo;
if TDataSource(Sender).DataSet.Active
then
FRecNo :=
TDataSource(Sender).DataSet.RecNo;
FKeyShift :=
False;
end;
procedure TFamEditPeople.DBGrid1MouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
I: Integer;
begin
if Button <> mbLeft
then
Exit;
if not TDBGrid(Sender).DataSource.DataSet.Active
then
Exit;
if FKeyShift
then begin
FRecNo :=
TDBGrid(Sender).DataSource.DataSet.RecNo;
if FKeyShift
then DBGrid1.SelectedRows.CurrentRowSelected :=
True;
if FOldNo = -
1 then
FOldNo :=
1;
if FRecNo > FOldNo
then
for I := FRecNo
downto FOldNo
do begin
TDBGrid(Sender).DataSource.DataSet.RecNo :=
I;
TDBGrid(Sender).SelectedRows.CurrentRowSelected :=
True;
end
else
for I := FRecNo
to FOldNo
do begin
TDBGrid(Sender).DataSource.DataSet.RecNo :=
I;
TDBGrid(Sender).SelectedRows.CurrentRowSelected :=
True;
end;
end
else//Shift not pressed
FOldNo :=
TDBGrid(Sender).DataSource.DataSet.RecNo
end;
实现DBGrid Shift+
鼠标左键单击 多选
关键字: DBGrid Shift 多选
分类 : 个人专区
密级 : 公开
(评分: , 回复: 0, 阅读:
96) »&
raquo;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,
Graphics, Controls, Forms, Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls;
type
TForm1 =
class(TForm)
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
Button1: TButton;
ADOTable1: TADOTable;
procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
blSelect: Boolean;
BookMark: TBookMark;
CurrNo, OldNo: integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin //实现 Shift+鼠标左键单击 多选
if Button = mbLeft
then
begin
if not blSelect
then
begin
BookMark :=
ADOTable1.GetBookMark;
OldNo :=
ADOTable1.RecNo;
blSelect :=
True;
Exit;
end
else
begin
if ssShift
in Shift
then
begin
CurrNo :=
ADOTable1.RecNo;
ADOTable1.DisableControls;
ADOTable1.GotoBookmark(BookMark);
DBGrid1.SelectedRows.CurrentRowSelected :=
True;
if CurrNo > OldNo
then
begin
while CurrNo > ADOTable1.RecNo
do
begin
DBGrid1.SelectedRows.CurrentRowSelected :=
True;
ADOTable1.Next;
end;
end
else
begin
while CurrNo < ADOTable1.RecNo
do
begin
DBGrid1.SelectedRows.CurrentRowSelected :=
True;
ADOTable1.Prior;
end;
end;
ADOTable1.EnableControls;
ADOTable1.FreeBookmark(BookMark);
blSelect :=
False;
CurrNo :=
0;
OldNo :=
0;
end
else
begin
BookMark :=
ADOTable1.GetBookMark;
OldNo :=
ADOTable1.RecNo;
blSelect :=
True;
Exit;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ADOConnection1.Connected :=
true;
ADOTable1.Close;
ADOTable1.TableName :=
‘Table_Name‘;
//表名
ADOTable1.Open;
DBGrid1.Options := DBGrid1.Options + [dgMultiSelect];
//开启多选
end;
end.
//另外,Shift+小键盘UP/DOWN 也可以实现多选
DBGrid中Shift多选
标签:select ctr one lse 键盘 window messages OLE lines