Forum > LCL
[resolved]DBGrid scrolls clientarea after working with dataset, how to prev..?
(1/1)
SunyD:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.BtnScrolltestClick(Sender: TObject);var aCurPos : TBookMark;begin Memo1.Lines.Add(TimeToStr(now)+' Scrolltest started ====='); aCurPos:=BufDs1.Bookmark; //Save last Position BufDs1.DisableControls; try //do something with DB.Table BufDs1.First; while not BufDs1.EOF do begin BufDs1.Next; end; BufDs1.Bookmark:=aCurPos; //Locate last Position finally BufDs1.EnableControls; // <-- after this call, DBGrid scrolls, but it schouln't // because old datarow in viewarea is located end; Memo1.Lines.Add(TimeToStr(now)+' Scrolltest ended');end;
I uploaded screenshot before and after calculation and example project.
SunyD:
Oh i posted this in wrong section. :-[ This was a question not a suggestion.
rvk:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type TCrackDBGrid = class(TDBGrid);// ...x := TCrackDBGrid(Grid).VisibleRowCount - TCrackDBGrid(Grid).Row;After restoring the bookmarj you can restore the position in the DBGrid by
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---x := Grid.DataSource.DataSet.MoveBy(x);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:
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
Navigation
[0] Message Index