
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 := ;
OldNo := ;
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 也可以实现多选