Table of Contents

Scrolling & scroll bars

Pixel-smooth or cell-snapping, continuous or release-only redraws, programmatic scroll-to, custom-styled or external scroll bars.

Overview

Scrolling is configured through three top-level grid properties — ScrollMode, ScrollUpdate, and CustomScrollBars — plus a handful of API methods for programmatic control. The defaults match Excel: smooth pixel scrolling with continuous redraw and OS-native scroll bars.

Scroll mode

uses
  FMX.TMSFNCDataGrid;

procedure TForm1.SetScrollMode;
begin
  Grid.ScrollMode := gsmCellScrolling;
end;
Value Effect
gsmPixelScrolling Smooth pixel-by-pixel scrolling (default).
gsmCellScrolling Snaps so the top-left visible cell is always fully aligned.

Pick gsmCellScrolling when fractional row/column visibility looks odd — for example a grid that imitates a paper form.

Scroll update strategy

uses
  FMX.TMSFNCDataGrid;

procedure TForm1.SetScrollUpdate;
begin
  Grid.ScrollUpdate := gsuOnce;
end;
Value Effect
gsuContinuous Redraws while the user drags the scroll thumb (default).
gsuOnce Redraws only when the thumb is released.

Use gsuOnce on very large grids where continuous repaint costs become noticeable.

Programmatic scrolling

uses
  FMX.TMSFNCDataGrid;

procedure TForm1.ScrollProgrammatically;
begin
  Grid.Scroll(0, 500);            // pixel offset (h, v)
  Grid.GoToCell(MakeCell(3, 20)); // navigate to a specific cell
end;

procedure TForm1.ReadScrollPosition;
var
  H, V: Single;
begin
  H := Grid.GetHScrollValue;
  V := Grid.GetVScrollValue;
end;

procedure TForm1.RebuildKeepingScroll;
begin
  Grid.Root.SaveScrollPosition;
  Grid.LoadFromCSVData('data.csv');
  Grid.Root.RestoreScrollPosition;
end;

Scroll events

uses
  System.SysUtils, FMX.TMSFNCDataGrid;

procedure TForm1.GridHScroll(Sender: TObject; APosition: Single);
begin
  StatusBar1.SimpleText := Format('H-scroll: %.0f', [APosition]);
end;

procedure TForm1.GridVScroll(Sender: TObject; APosition: Single);
begin
  StatusBar1.SimpleText := Format('V-scroll: %.0f', [APosition]);
end;

Assign via OnHScroll and OnVScroll.

Custom scroll bars

Replace the OS scroll bars with the grid's own styled scroll bars (TTMSFNCScrollBar) for a consistent look across platforms:

uses
  FMX.TMSFNCScrollBar, FMX.TMSFNCDataGrid;

procedure TForm1.ConfigureCustomScrollBars;
var
  HSB, VSB: TTMSFNCScrollBar;
begin
  Grid.CustomScrollBars := True;

  // Access them directly
  HSB := Grid.CustomHorizontalScrollBar;
  VSB := Grid.CustomVerticalScrollBar;

  // Fill the full edge (touch the corners) instead of leaving a gap
  Grid.StretchScrollBars := True;

  // Hide a scroll bar without disabling scroll
  Grid.HorizontalScrollBarVisible := False;
  Grid.VerticalScrollBarVisible   := True;
end;

External scroll bars

For layouts where the scroll bars need to live outside the grid control (e.g. in a separate panel), implement ITMSFNCDataGridExternalScrollBar on any control and assign it:

uses
  FMX.TMSFNCDataGrid;

// MyVerticalScrollBarAdapter / MyHorizontalScrollBarAdapter are controls that
// implement ITMSFNCDataGridExternalScrollBar.
procedure TForm1.AssignExternalScrollBars;
begin
  Grid.ExternalVerticalScrollBar   := MyVerticalScrollBarAdapter;
  Grid.ExternalHorizontalScrollBar := MyHorizontalScrollBarAdapter;
end;

The interface requires: Value, Min, Max, PageSize, SmallChange, LargeChange, Visible, and OnChange. The grid drives all properties automatically; the implementation forwards them to the underlying scroll control.

Mouse-wheel scrolling

Grid.Options.Mouse controls wheel behaviour:

uses
  FMX.TMSFNCDataGrid;

procedure TForm1.ConfigureWheelScrolling;
begin
  Grid.Options.Mouse.WheelScrollSize          := 3;    // rows per tick
  Grid.Options.Mouse.WheelScrollKeepSelection := True; // don't move cursor on scroll
end;

Touch scrolling

uses
  FMX.TMSFNCDataGrid;

procedure TForm1.ConfigureTouchScrolling;
begin
  Grid.Options.Mouse.TouchScrolling            := True; // enable swipe scroll
  Grid.Options.Mouse.TouchScrollingSensitivity := 5;    // pixels before scroll starts
end;

Auto-scroll while dragging

When the user drags a selection (or a row/column) past the edge of the visible area, the grid auto-scrolls:

uses
  FMX.TMSFNCDataGrid;

procedure TForm1.ConfigureAutoScroll;
begin
  Grid.Options.Mouse.AutoScrolling         := True;
  Grid.Options.Mouse.AutoScrollingInterval := 50; // ms per tick
  Grid.Options.Mouse.AutoScrollingSpeed    := 2;  // rows/cols per tick
end;
  • Grid.ScrollMode, Grid.ScrollUpdate
  • Grid.Scroll(H, V), Grid.GoToCell(Cell)
  • Grid.GetHScrollValue / GetVScrollValue
  • Grid.Root.SaveScrollPosition / RestoreScrollPosition
  • OnHScroll, OnVScroll
  • Grid.CustomScrollBars, StretchScrollBars
  • Grid.HorizontalScrollBarVisible, Grid.VerticalScrollBarVisible
  • Grid.CustomHorizontalScrollBar, Grid.CustomVerticalScrollBar
  • Grid.ExternalHorizontalScrollBar, Grid.ExternalVerticalScrollBar
  • Grid.Options.Mouse.WheelScrollSize, WheelScrollKeepSelection
  • Grid.Options.Mouse.TouchScrolling, TouchScrollingSensitivity
  • Grid.Options.Mouse.AutoScrolling, AutoScrollingInterval, AutoScrollingSpeed

See also

  • Large datasetsgsuOnce and other patterns for big grids.
  • Keyboard & mouse — page-up/page-down navigation also drives scrolling.
  • Paging — paging is an alternative to long-scroll datasets.