* * *

Author Topic: [resolved]DBGrid scrolls clientarea after working with dataset, how to prev..?  (Read 590 times)

SunyD

  • Guest
I have a dataset connected with dbgrid. Sometimes I must scroll the dataset from first to last row to calculate data.
I mark the current position of the data row and restore it after calculation.
It is still same row selected, but the dbgrid scrolls view area to up or downwards.
In big tables with to much columns it is frustrating, the user thinks it is another data row is located.

How to prevent this, it is bug from dbgrid or it is my failure?

This ist the sample code, please read the comments:
Code: Pascal  [Select]
  1. procedure TForm1.BtnScrolltestClick(Sender: TObject);
  2. var aCurPos : TBookMark;
  3. begin
  4.   Memo1.Lines.Add(TimeToStr(now)+' Scrolltest started =====');
  5.   aCurPos:=BufDs1.Bookmark;   //Save last Position
  6.  
  7.   BufDs1.DisableControls;
  8.  try
  9.   //do something with DB.Table
  10.   BufDs1.First;
  11.   while not BufDs1.EOF do begin
  12.     BufDs1.Next;
  13.   end;
  14.  
  15.   BufDs1.Bookmark:=aCurPos;   //Locate last Position
  16.  finally
  17.    BufDs1.EnableControls;     // <-- after this call, DBGrid scrolls, but it schouln't
  18.                               // because old datarow in viewarea is located
  19.  end;
  20.   Memo1.Lines.Add(TimeToStr(now)+' Scrolltest ended');
  21. end;
  22.  

I uploaded screenshot before and after calculation and example project.
« Last Edit: October 11, 2017, 12:53:13 pm by Soner A. »

SunyD

  • Guest
Oh i posted this in wrong section. :-[  This was a question not a suggestion.

rvk

  • Hero Member
  • *****
  • Posts: 2974
Yeah, the position within the grid isn't restored there. With BufDs1.Bookmark you are just restoring the dataset to the correct position. Not the TDBGrid.

I've done this in Delphi with getting the current row within the visible grid.

For instance, this will save the numbers of rows FROM THE BOTTOM of the grid.
Code: Pascal  [Select]
  1. type
  2.   TCrackDBGrid = class(TDBGrid);
  3. // ...
  4. x := TCrackDBGrid(Grid).VisibleRowCount - TCrackDBGrid(Grid).Row;
After restoring the bookmarj you can restore the position in the DBGrid by
Code: Pascal  [Select]
  1. x := Grid.DataSource.DataSet.MoveBy(x);
  2. x := Grid.DataSource.DataSet.MoveBy(-x);
The first line will move x records further scrolling bottom of the DBGrid to the exact position needed. The second scrolls back to your position. After that your grid will be exactly as before.

(Note: This was done by me for Delphi in the past. I'm not sure if there is a better method in the meantime.)

SunyD

  • Guest
Thanks, it works!

Real solution can be ignoring dataset-scroll-events in DBGrid. Until I find better solution in Dbgrid.pas I will use your solution.

Thanks again.  Adios dancing dbgrids  :D

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus