在實際項目中,偶然遇到須要按下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 := 0; FOldNo := 0; 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;