Shift DBGrid in multiple-choice

[dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, 
dgTabs, dgRowSelect, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]

相关代码
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  the begin 
        the TDBGrid (Sender) .DataSource.DataSet.RecNo: = the I; 
        the TDBGrid (Sender) .SelectedRows.CurrentRowSelected: = True;
       End ;
   End 
  the else // the Shift not pressed 
    FOldNo: = the TDBGrid (Sender) .DataSource.DataSet.RecNo
 End ; 



achieve the Shift DBGrid + left mouse button click multiple choice 
keywords: DBGrid Shift multiple-choice 
Category: personal Cabinet 
Confidentiality: public 
(Rating:, replies: 0 , read: 96 ) & raquo; & 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 
  the Form1: TForm1; 

Implementation 

{ $ R & lt *. Dfm } 

Procedure TForm1.DBGrid1MouseUp (Sender: TObject; the Button: TMouseButton; 
  the Shift: TShiftState; X-, the Y: Integer); 
the begin  // achieve Shift + mouse left-click multiple choice 
  IF the button = mbLeft the then 
  the begin 
    IF  not blSelect the then 
    the begin 
      a 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; 
        the Exit; 
      End ;
     End ;
   End ;
 End ; 

Procedure TForm1.FormCreate (Sender: TObject);
 the begin 
  ADOConnection1.Connected: = to true; 
  ADOTable1.Close; 
  ADOTable1.TableName: = ' Table_Name ' ; // table 
  ADOTable1.Open ; 
  DBGrid1.Options: = DBGrid1.Options + [dgMultiSelect]; // open multiple choice 
End ; 

End . 

// Further, Shift + keypad UP / DOWN may be selected from the multi-  

 

Guess you like

Origin www.cnblogs.com/jijm123/p/12364713.html