Multi select rows in a DBGrid using Shift (select as in the IE)

Spread the love

//Folgendes muss eingebaut werden

property BM1: TBookmark read FBM1 Write SetBM1;

property BM2: TBookmark read FBM2 Write SetBM2;

procedure Markieren(Sender: TObject);
  function Shiftgedr: Boolean;


    procedure TForm1.Markieren(Sender: TObject);
    var
      Richtung: string;
      TempBM: TBookmark;
    begin
      with (Sender as TDBGRID).DataSource.Dataset do
      begin
        if (BOF and EOF) then
          Exit;
        DisableControls;
        try
          try
            GotoBookmark(BM1);
            case DBGrid1.DataSource.DataSet.CompareBookmarks(BM1, BM2) of
                -1: Richtung := 'Unten';
              1: Richtung    := 'Oben';
              0: Richtung    := 'Gleich';
            end;
            TempBM := DBGrid1.DataSource.DataSet.GetBookmark;
            while DBGrid1.DataSource.DataSet.CompareBookmarks(BM2, TempBM) <> 0 do
            begin
              DBGrid1.SelectedRows.CurrentRowSelected := True;
              if Richtung = 'Unten' then
                Next
              else
                Prior;
              TempBM := DBGrid1.DataSource.DataSet.GetBookmark;
            end;
          finally
            FreeBookmark(tempbm);
          end;
        finally
          EnableControls;
        end;
      end;
    end;

    function TForm1.Shiftgedr: Boolean;
    var
      State: TKeyboardState;
    begin
      GetKeyboardState(State);
      Result := ((State[VK_SHIFT] and 128) <> 0);
    end;

    //Im "OnMouseUP" des Grids folgenden Code einbauen
  begin
    if not Shiftgedr then
      Merke1 := nil;

    if Merke1 = nil then
      Merke1 := DBGrid1.DataSource.DataSet.GetBookmark
    else
      Merke2 := DBGrid1.DataSource.DataSet.GetBookmark;

    if (Merke1 <> nil) and (Merke2 <> nil) then
    begin
      if Shiftgedr then
      begin
        Markieren(Sender);
      end;
    end;
  end;