Question

New to delphi and database programming in general but am curious if there is a better way to swap records in a TDataset? I have read through some help and cant find any obvious methods. Currently I have a procedure implemented to move records down the dataset until they hit the Eof marker. However I am getting some odd errors when I get to the last record in my data. All I have is implemented a standard array-style swap routine trying to preserve data and whatnot while juggling active records.

Code So Far

procedure TForm2.btnDownClick(Sender: TObject);
var
   sTmp,sTmp2  : string;
   iTmp,iTmp2  : integer;
begin
   tblMatched.DisableControls;
   if ( tblMatched.Eof <> true ) then
   begin
      // Grab data to swap
      tblMatched.GotoBookmark( tblMatched.GetBookmark );
      iTmp := tblMatched.Fields[0].AsInteger;
      sTmp := tblMatched.Fields[1].AsString;
      tblMatched.Next;
      iTmp2 := tblMatched.Fields[0].AsInteger;
      sTmp2 := tblMatched.Fields[1].AsString;

      // Swap data
      tblMatched.Prior;
      tblMatched.Edit;
      tblMatched.Fields[0].Value := iTmp2;
      tblMatched.Fields[1].Value := sTmp2;

      tblMatched.Next;
      tblMatched.Edit;
      tblMatched.Fields[0].AsInteger := iTmp;
      tblMatched.Fields[1].AsString := sTmp;
   end;
   tblMatched.EnableControls;
end;
Was it helpful?

Solution

It looks like you're using an in-memory dataset, such as TClientDataset. If you simply put an index on the dataset, it will keep things ordered for you so you don't have to rearrange them manually. Just set up the index based on whatever criteria you want to have it use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top