# Grid Virtual Scrolling

The Grid virtual scrolling feature allows users to scroll vertically through all records in the Grid data source. The feature is an alternative to paging.

To enhance the rendering performance, the Grid reuses the same set of HTML elements. Loading indicators (skeletons) appear in the table cells during scrolling and data loading. If the user scrolls back up after scrolling down to the next set of rows, the previous data reloads from the data source, similar to regular paging, with the scroll distance determining the data to be loaded.

You can also use the [Blazor Grid virtualization for the Grid columns](https://www.telerik.com/blazor-ui/documentation/components/grid/columns/virtual).

## Using Virtual Scrolling

To enable [Blazor Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/overview) row virtualization:

1. Set the `ScrollMode` parameter to `GridScrollMode.Virtual` (the default value is `Scrollable`).
2. [Set the `Height` parameter](https://www.telerik.com/blazor-ui/documentation/components/grid/virtual-scrolling#height) to a `string` CSS value.
3. [Set the `RowHeight` parameter](https://www.telerik.com/blazor-ui/documentation/components/grid/virtual-scrolling#rowheight) to a `decimal` value that denotes pixels.
4. [Set the `PageSize` parameter](https://www.telerik.com/blazor-ui/documentation/components/grid/virtual-scrolling#pagesize).

> The values of the `Height`, `RowHeight`, and `PageSize` parameters are related to one another. The following sections explain how.

## Height

Set the Grid `Height` parameter to any [valid `string` CSS value](https://www.telerik.com/blazor-ui/documentation/common-features/dimensions), for example, `px`, `%`, `em`, or `vh`. If the Grid should expand vertically, accoding to the available space, then check the article [Adjust Grid Height to Match the Browser Viewport Height](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-adjust-height-with-browser).

Set the `Height` value, so that users can't see the whole [`PageSize` of items](https://www.telerik.com/blazor-ui/documentation/components/grid/virtual-scrolling#pagesize) at once. Otherwise, empty row skeletons may display in the Grid while users are not scrolling.

## PageSize

Set the Grid `PageSize` parameter to an `int` value. The `PageSize` determines:

* How many table rows are populated and rendered at any given time.
* How many data items are requested from the data source when using the [Grid `OnRead` event to load data on demand](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations).

Set the `PageSize` value, so that the rendered table rows do fit in the [Grid height](https://www.telerik.com/blazor-ui/documentation/components/grid/virtual-scrolling#height). At least one table row must be completely invisible. Otherwise, empty row skeletons may display in the Grid while users are not scrolling. The exact `PageSize` value allows you to balance between better user experience and data request efficiency:

* A larger `PageSize` value will make the Grid display empty row skeletons more rarely while users are scrolling down. At the same time, the Grid may be requesting a larger number of data items repetitively.
* A smaller `PageSize` will make the Grid request a smaller number of items on each user scroll. At the same time, users will see row skeletons sooner or more frequently during scrolling.

> The `PageSize` value does not affect the data request frequency. The Grid `OnRead` event always fires when the user stops scrolling, no matter what data is currently available.

## RowHeight

Set the `RowHeight` parameter to a `decimal` value. The Grid uses it to set an inline `height` style in pixels to all Grid table rows (`<tr>`).

The `RowHeight` value must be large enough to accommodate the cell content in all rows, even if the content differs or wraps. In other words, the `RowHeight` setting must apply the same or greater table row height than what the browser would normally render. The effective row height depends on:

* The cell content and [text wrapping](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-rows-text-ellipsis).
* The [`Size` parameter value](https://www.telerik.com/blazor-ui/documentation/components/grid/sizing).
* The CSS theme, including font size, line height, and cell paddings.

For example, the following list shows the minimum valid `RowHeight` values when using the [built-in CSS themes](https://www.telerik.com/blazor-ui/documentation/styling-and-themes/overview), single-line plain text content, no command buttons, and [`Medium` `Size`](https://www.telerik.com/blazor-ui/documentation/components/grid/sizing):

* `36` for the Default theme (`14px` font size, `20px` line height, and 2 \* `8px` vertical paddings)
* `40` for the Bootstrap theme (`16px` font size, `24px` line height, and 2 \* `8px` vertical paddings)
* `45` for the Material theme (`14px` font size, `20px` line height, 2 \* `12px` vertical paddings, and `1px` bottom border)
* `41` for the Fluent theme (`14px` font size, `20px` font size and 2 \* `10px` vertical paddings, and `1px` bottom border)
* `47` for the Meridian theme (`16px` font size, `24px` line height, 2 \* `11px` vertical paddings, and `1px` bottom border)

> Browsers treat table row `height` styles as `min-height` styles. If the table row content cannot fit in the set `RowHeight`, the browser expands the table row. The Grid configuration must not allow this to happen. It is crucial that all Grid table rows display with the same effective height when using virtial scrolling, otherwise the virtual scrolling experience will break. You may need to [disable text wrapping in the Grid data cells to achieve this requirement](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-rows-text-ellipsis).

The `RowHeight` parameter value cannot change at runtime, unless the application recreates the whole Grid component by removing it from the web page temporarily.

If necessary, you can also use the `RowHeight` parameter without virtual row scrolling.

## Limitations

The [Blazor Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/overview) virtualization enhances client-side rendering performance and improves the user experience by providing quicker access to all data items. However, this comes with the trade-offs:

* [Hierarchy](https://www.telerik.com/blazor-ui/documentation/components/grid/hierarchy) is not supported. If the Grid hierarchy is self-referencing, use a [TreeList with virtual scrolling](https://www.telerik.com/blazor-ui/documentation/components/treelist/virtual-scrolling) instead.
* [Grouping](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/overview) is supported only when [loading groups on demand](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/load-on-demand).
* There is a [browser limitation, which affects the maximum number of data items in a virtual Grid](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-virtualization-many-records). The problem occurs with millions of items and you can partially mitigate it by [changing the Grid styles to make the row height smaller](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-reduce-row-height).

In addition to virtual scrolling, another approach to optimize the app rendering and data request performance is to use [Grid paging](https://www.telerik.com/blazor-ui/documentation/components/grid/paging) and [`OnRead` event](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread).

## Elastic Scrolling

Virtual scrolling triggers data requests based on the scroll offset of the Grid data area. Some browsers allow users to scroll up or down beyond the limits of the scrollable content. This UX feature is called *elastic scrolling* and can cause undesired effects such as:

* Data requests with a negative or too large `Skip` value.
* Unexpected row placeholders at presumably unreachable scroll offsets.

You can [disable elastic scrolling with CSS](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-negative-skip-value-virtual-scrolling).

## Example

Row virtualization is often used with a large number of data items that cannot be loaded in a single request. Thus, the example below uses the [Grid `OnRead` event](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations) together with the [`ToDataSourceResultAsync()`](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#todatasourceresult-method) method. You can also use the Grid `Data` parameter and load all data items with a single request. [Do not use `Data` and `OnRead` at the same time](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/overview#how-to-provide-data).

If you use the `OnRead` event without `ToDataSourceResultAsync()`, then [use the `Skip` and `PageSize` values](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations#virtual-scrolling-with-onread) of the [`DataSourceRequest` argument](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#event-argument) to determine the Grid scroll offset and load the correct data items. When grouping is enabled, the `Skip` value is always `0` and the `DataSourceRequest` provides [information about the requested data in other properties](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/load-on-demand#server-operations).

> caption Virtual Grid scrolling with optional OnRead event and grouping

<demo metaUrl="client/grid/virtual-scrolling/" height="500"></demo>

## See Also

* [Live Demo: Grid Virtual Scrolling](https://demos.telerik.com/blazor-ui/grid/virtual-scrolling)
* [Grid Selection with Virtual Rows](https://www.telerik.com/blazor-ui/documentation/components/grid/selection/rows#selection-and-virtual-scrolling)
* [How to Disable Row Placeholders During Virtual Scrolling](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-hide-virtual-row-skeletons)
