In our application there are 5 grids below each other. Each grid has the same columns as all the other grids. I would like to be able to synchronize all grids horizontally when 1 grid scrolls. Is that possible and if so how do I accomplish that?
I think I found the solution:
procedure TfrmTIJDVERANTWOORDINGENCustomBrowser.grdNonConsolidatedPrjTopLeftChanged(Sender: TObject);
procedure lpScrollColumnIntoView(grid, evntGrid: TAdvStringGrid; col, row: integer);
begin
if evntGrid <> grid then
begin
// grid.BeginUpdate;
try
grid.LeftCol := col;
grid.TopRow := row;
finally
// grid.EndUpdate;
end;
end;
end;
begin
var grid := TAdvStringGrid(Sender);
var col := grid.LeftCol;
var row := grid.TopRow;
lpScrollColumnIntoView(grdNonConsolidatedPrj, grid, col, row);
lpScrollColumnIntoView(grdNonConsolidatedPrjSum, grid, col, row);
lpScrollColumnIntoView(grdNonConsolidatedAlg, grid, col, row);
lpScrollColumnIntoView(grdNonConsolidatedAlgSum, grid, col, row);
lpScrollColumnIntoView(grdNonConsolidatedSum, grid, col, row);
end;
No code solution
grid1.SyncGrid.Grid := grid2;
grid2.SyncGrid.Grid := grid3;
..
gridn.SyncGrid.Grid := grid1;
and set for all grids: grid.SyncGrid.ScrollVertical = true
That, I think, is nice but only works oneway. Scrolling grid 1 and 2 will not scroll gridn. Am I right?
Final version:
/// <summary>
/// The Top row and/or left column have changed.
/// </summary>
procedure TfrmTIJDVERANTWOORDINGENCustomBrowser.TopLeftChanged(Sender: TObject);
/// <summary>
/// Synchronize the scroll position of a grid.
/// </summary>
procedure lpScrollGrid(grid, evntGrid: TAdvStringGrid; col: integer);
begin
if evntGrid <> grid then
grid.LeftCol := col;
end;
begin
var grid := TAdvStringGrid(Sender);
var col := grid.LeftCol;
lpScrollGrid(grdNonConsolidatedPrj, grid, col);
lpScrollGrid(grdNonConsolidatedPrjSum, grid, col);
lpScrollGrid(grdNonConsolidatedAlg, grid, col);
lpScrollGrid(grdNonConsolidatedAlgSum, grid, col);
lpScrollGrid(grdNonConsolidatedSum, grid, col);
end;
If you try this example and connect in loop, you will see it works in all ways.
Yep, tried it, works fine indeed. Thank you for this solution.
B.t.w. had to change ScrollVertical := true to ScrollHorizontal := true.