在实际项目中,偶然遇到需要按下SHIFT键,在DBGrid中进行多选的情况,测试了几种方法,最终确定了一个比较好的解决方法,总结如下:
procedure TTestFrame.TestDBGridMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin //实现 Shift+鼠标左键单击 多选
if Button = mbLeft then
begin
if not FblSelect then
begin
FBookMark := TDBGrid(Sender).DataSource.DataSet.GetBookMark;
FOldNo := TDBGrid(Sender).DataSource.DataSet.RecNo;
FblSelect := True;
Exit;
end
else
begin
if ssShift in Shift then
begin
FCurrNo := TDBGrid(Sender).DataSource.DataSet.RecNo;
TDBGrid(Sender).DataSource.DataSet.DisableControls;
TDBGrid(Sender).DataSource.DataSet.GotoBookmark(FBookMark);
TDBGrid(Sender).SelectedRows.CurrentRowSelected := True;
if FCurrNo > FOldNo then
begin
while FCurrNo > TDBGrid(Sender).DataSource.DataSet.RecNo do
begin
TDBGrid(Sender).SelectedRows.CurrentRowSelected := True;
TDBGrid(Sender).DataSource.DataSet.Next;
end;
end
else
begin
while FCurrNo < TDBGrid(Sender).DataSource.DataSet.RecNo do
begin
TDBGrid(Sender).SelectedRows.CurrentRowSelected := True;
TDBGrid(Sender).DataSource.DataSet.Prior;
end;
end;
TDBGrid(Sender).DataSource.DataSet.EnableControls;
TDBGrid(Sender).DataSource.DataSet.FreeBookmark(FBookMark);
FblSelect := False;
FCurrNo := ;
FOldNo := ;
TDBGrid(Sender).Invalidate;
end
else
begin
FBookMark := TDBGrid(Sender).DataSource.DataSet.GetBookMark;
FOldNo := TDBGrid(Sender).DataSource.DataSet.RecNo;
FblSelect := True;
Exit;
end;
end;
end;
end;