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

半城伤御伤魂 提交于 2020-01-02 02:48:07

问题


I need to refresh dbgrid constantly, in real time. Close and open dataset works fine, but blink the dbgrid. What can I do to avoid this?

I'd like a solution like Ajax, that update only the necessary.

Thanks


回答1:


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.




回答2:


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




回答3:


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;


来源:https://stackoverflow.com/questions/2311007/how-to-refresh-dbgrid-without-close-and-open-dataset-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!