Einzelnen Beitrag anzeigen

HolgerCW

Registriert seit: 28. Nov 2006
Ort: Marl
1.207 Beiträge
 
Delphi XE7 Enterprise
 
#13

AW: RecNo gibt ergibt immer -1

  Alt 20. Mai 2021, 16:55
Hallo zusammen,

vielen Dank für Euren Input. nun habe ich es hinbekommen.

Zitat:
procedure TMainForm.DBGridMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ShowMessage(IntToStr((Sender as TDBGrid).DataSource.DataSet.RecNo));
end;
Das ging leider nicht

Zitat:
Das wird eigentlich vom TDBGrid (ohne jedewede weitere Programmierung) unterstützt.
Das ist nur bei dem TJVDBGrid so

So hat es geklappt:

Code:
procedure TForm1.DBG1MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

 if ssShift in Shift then
  SetBookMarkRange(Sender as TDBGrid,DM1.Query1);

end;

-----------------


procedure TDM1.Query1AfterScroll(DataSet: TDataSet);
begin

 if SettingBookmarkRange then exit;

 PreviousRowID := CurrentRowID;
 CurrentRowID := DM1.Query1.FieldByName('ID').AsInteger;

end;


----------------

var
 SettingBookmarkRange: Boolean;
 CurrentRowID,
 PreviousRowID: Integer;

procedure SetBookmarkRange(DBGrid_Shift:TDBGrid;Query_Shift:TQuery);
var
 BM,
 TempBM : TBookmark;
 NewBM : TBookmarkStr;
 FoundPrevious : Boolean;
begin

  //  This code is called after the user does a Shift-Click in the grid
  //  First we set a flag to temporarily prevent the CurrrentRowID and
  //  PreviousrowID from being updated during the dataset's OnScroll event
  SettingBookmarkRange := True;

  BM := Query_Shift.GetBookmark;

  //  Set a flag to keep track of whether we've found the row with the PreviousRowID
  FoundPrevious := False;
  try

   Query_Shift.DisableControls;

   //  First, search forwards to see if we can find the the row with the PreviousRowID
   //  In other words, we're first checking that the Shift-Click was on a row *above*
   //  the previous row
   Query_Shift.Next;

   while not FoundPrevious and not Query_Shift.Eof do
   begin

    if Query_Shift.FieldByName('ID').AsInteger = PreviousRowID then
    begin

     FoundPrevious := True;

     //  We found the row with the PreviousRowID, so now get the Grid to add it, and
     //  all the rows back to where we started, in its SelectedRows list
     while not Query_Shift.Bof and (Query_Shift.FieldByName('ID').AsInteger <> CurrentRowID) do
     begin

      DBGrid_Shift.SelectedRows.CurrentRowSelected := True;
      Query_Shift.Prior;

     end;

    end
    else

     Query_Shift.Next;

    end;

    if not FoundPrevious then
    begin
     //  If we get here, the Shift-Click must have been in a row further down the
     //  grid than the previously-current one
     while not FoundPrevious and not Query_Shift.Bof do
     begin

      if Query_Shift.FieldByName('ID').AsInteger = PreviousRowID then
      begin

       FoundPrevious := True;
       //  We found the row with the PreviousRowID, so now get the Grid to add it, and
       //  all the rows back to where we started, in its SelectedRows list
       while not Query_Shift.Eof and (Query_Shift.FieldByName('ID').AsInteger <> CurrentRowID) do
       begin

        DBGrid_Shift.SelectedRows.CurrentRowSelected := True;
        Query_Shift.Next;

       end;

      end
      else

       Query_Shift.Prior;

      end;

    end;

  finally

   Query_Shift.GotoBookmark(BM);
   Query_Shift.FreeBookmark(BM);
   Query_Shift.EnableControls;
   SettingBookmarkRange := False;

  end;

end;
Gruss

Holger
  Mit Zitat antworten Zitat