Delete and refresh a record in DBgrid where u main

2019-09-14 21:47发布

问题:

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 !

回答1:

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;