How to refresh dbgrid without close and open dataset in delphi?

非 Y 不嫁゛ 提交于 2019-12-05 04:14:42

Have you tried to use Disable- & EnableControls?

DataSet.DisableControls;
try
  DataSet.Close;
  DataSet.Open;
finally
  DataSet.EnableControls;
end;

Furthermore, it should be possible to just call DataSet.Refresh instead of closing and opening to get the same result.

I use this in my app

DataSet.MergeChangeLog;
DataSet.ApplyUpdates(-1);
DataSet.Refresh;

The above code is in an action named actRefreshData, in the ActionManager

When I need to use I just call it like

actRefreshData.Execute;

Hope this helps.

Hint: you can add a Timer and automate this

Look here:

type THackDataSet=class(TDataSet); // a nice "hack" so we can access 
//protected members
 THackDBGrid=class(TDBGrid);

procedure {tdmdb.}refreshgrid(grid : tdbgrid);

var row, recno : integer;
    ds : tdataset;
    b : tbookmark;
begin
  Row := THackDBGrid(grid).Row;// or THackDataSet(ds).ActiveRecord

  ds := grid.datasource.dataset;

  RecNo := ds.RecNo;

  b := ds.GetBookmark;

  try
    ds.close;
    ds.Open;
  finally
    if (b<>nil) and ds.BookMarkValid(b) then
    try
     //      ds.GotoBookMark(b);
      ds.CheckBrowseMode;
      THackDataSet(ds).DoBeforeScroll;
      THackDataSet(ds).InternalGotoBookmark(b);
      if THackDataSet(ds).ActiveRecord <> Row - 1 then
      THackDataSet(ds).MoveBy(Row - THackDataSet(ds).ActiveRecord - 1);
      ds.Resync([rmExact{, rmCenter}]);
      THackDataSet(ds).DoAfterScroll;
    finally
      ds.FreeBookMark(b);
    end
    else if (recno<ds.RecordCount) and (recno<>ds.RecNo) then
    begin
      ds.First;
      ds.MoveBy(Max(0, recno-1));
    end;
  end;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!