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

3

7

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

Questionable answered 22/2, 2010 at 13:9 Comment(0)
C
14

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.

Collard answered 22/2, 2010 at 13:19 Comment(2)
Good advice! I had forgotten that.Questionable
If the dataset is a query, the refresh does not work. There is also another problem with this code, the dbgrid changes the scroll position. How to make dbgrid not to change it's scroll?Olivette
S
0

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

Salomo answered 5/11, 2016 at 21:30 Comment(0)
D
0

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;
Davidson answered 22/8, 2017 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.