Search code examples
databasedelphidelphi-xe2recorddbgrid

Delete and refresh a record in DBgrid where u maintain the same position


I have a small database I'm using dbgo, I have a DBgrid displaying my records, I need to know how to delete a record and refresh the database where the index arrow stays in the same position or at least go to the next? but currently my index arrow jump to start form the beginning each time I refresh !


Solution

  • Just keep and reset Recno

    var
    I:Integer;
    .......
    
    I := Ads.Recno;
    Ads.Delete;
    Ads.Recno := I;
    

    an example implementation for usage with DBNavigator could be

    Procedure DeleteAndKeepRecno(Ads: TCustomAdoDataset);
    var
      rn: Integer;
    begin
      rn := Ads.RecNo;
      Ads.Delete;
      Ads.RecNo := rn;
    end;
    
    procedure TForm4.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
    begin
      if Button = nbDelete then
      begin
        DeleteAndKeepRecno (TCustomAdoDataset(TDBNavigator(Sender).DataSource.DataSet));
        Abort;
      end;
    end;