# Blazor Grid Virtualization vs Paging

URL: https://flexcoreui.com/demo/grid/virtualization-bench  
Section: Data Grid  
Controls: GridControl, GridColumn  
Keywords: paging, pagination, pager, virtualization vs paging, page size

Row window or paging? The same 1,000 rows in a Blazor grid three ways, with the build and render time of each measured on the server.

## Documentation

### Grid

| Name | Type | Default | Description |
|---|---|---|---|
| AllowPaging | bool | false | Renders one page of rows and a pager instead of the scrolling window. |
| PageSize | int | 50 | Rows per page. |
| PageSizes | int[] | 25, 50, 100, 200 | Page sizes offered in the pager. |
| PagerPosition | GridPagerPosition | Bottom | Where the pager sits: Bottom, Top or Both. |
| BatchEditBehavior | GridBatchEditBehavior | MultiRow | MultiRow: a single click enters edit and the committed value fans out to the selected rows. SingleCell: a click edits one cell and typed keys commit to it on Enter. |
| ShowRowSelectionEditColumnCue | bool | false | In row-selection grids, highlights the editable target column across the selected rows. |
| ShowRowCountInFooter | bool | false | A compact row count under the grid: the complete filtered result, not the rendered window or page. |
| ShowSearchBar | bool | false | A search box in the grid toolbar. |
| EnableTypeSearch | bool | false | With the grid focused, printable keys search the active column for the first row whose text starts with the typed buffer. |
| AllowGrouping | bool | false | Lets the user group by a column; the group drop area appears above the header. |
| SelectionSettingsRef | SelectionSettings? | null | Selection options object: Multiple rows here. |
| EditSettingsRef | EditSettings? | null | Editing options object: batch mode here. |
| EventsRef | GridControlEvents<TValue>? | null | Event callbacks object; two of them are wired here. |

### Events

| Name | Type | Description |
|---|---|---|
| PageChanging | PageChangeEventArgs | Before the page changes; PreviousPage, CurrentPage, and Cancel to stay. |
| PageChanged | PageChangeEventArgs | After the page changed; PreviousPage and CurrentPage. |
| OnCellSave | CellSaveArgs<TValue> | After a cell's new value has been written to its row: a real change, or an editor the user touched. |

### Methods

| Name | Type | Description |
|---|---|---|
| GoToPageAsync(int page) | Task | Moves the pager to a page. |

## Examples

### Modes

```razor
@using System.Diagnostics
@using Fx.ControlKit
@using Fx.ControlKit.Grid

<ButtonGroupControl Items="_modeItems" Values="@(new[] { _modeKey })" ValuesChanged="PickMode" Selection="ChoiceSelectionMode.Single" AriaLabel="Scroll or page" />

<GridControl @key="@_mountGen"
             TValue="VRow"
             DataSource="@_rows"
             Height="460px"
             AllowPaging="@_paging"
             PageSize="@_pageSize"
             PageSizes="@(new[] { 25, 100 })"
             AllowSelection="true"
             AllowSorting="true"
             AllowFiltering="true"
             AllowGrouping="true"
             AllowResizing="true"
             AllowColumnReorder="true"
             ShowSearchBar="true"
             EnableTypeSearch="true"
             ShowRowSelectionEditColumnCue="true"
             ShowRowCountInFooter="true"
             SelectionSettingsRef="@_selection"
             EditSettingsRef="@_edit"
             EventsRef="@_events">
    <GridColumnsBase>
        <GridColumn Field="@nameof(VRow.Code)" HeaderText="Code" Width="95px" AllowEditing="false" />
        <GridColumn Field="@nameof(VRow.Description)" HeaderText="Description" Width="260px" />
        <GridColumn Field="@nameof(VRow.Category)" HeaderText="Category" Width="140px" />
        <GridColumn Field="@nameof(VRow.Qty)" HeaderText="Quantity" Width="95px" Type="ColumnType.Number" TextAlign="TextAlign.Right" SelectAllOnEdit="true" />
        <GridColumn Field="@nameof(VRow.Price)" HeaderText="Unit Price" Width="110px" Type="ColumnType.Number" Format="N2" TextAlign="TextAlign.Right" SelectAllOnEdit="true" />
        <GridColumn Field="@nameof(VRow.Total)" HeaderText="Total Cost" Width="120px" Type="ColumnType.Number" Format="C2" TextAlign="TextAlign.Right" AllowEditing="false" />
        <GridColumn Field="@nameof(VRow.Available)" HeaderText="Available" Width="85px" Type="ColumnType.Boolean" TextAlign="TextAlign.Center" />
    </GridColumnsBase>
</GridControl>
<p class="result">@Summary</p>
```

```csharp
public class VRow
{
    public string Code { get; set; } = "";
    public string Description { get; set; } = "";
    public string Category { get; set; } = "";
    public decimal Qty { get; set; }
    public decimal Price { get; set; }
    public decimal Total => Qty * Price;
    public bool Available { get; set; }
}

private readonly SelectionItem[] _modeItems = { new("scroll", "Scroll"), new("page25", "25 per page"), new("page100", "100 per page") };

private List<VRow> _rows = [];

private string _modeKey = "scroll";

private bool _paging;

private int _pageSize = 25;

private int _mountGen;                       // @key: every switch remounts the grid, so each timing starts from an empty grid

private readonly SelectionSettings _selection = new() { Type = SelectionType.Multiple, Mode = SelectionMode.Row };

private readonly EditSettings _edit = new() { AllowEditing = true, Mode = EditMode.Batch };

private readonly GridControlEvents<VRow> _events = new();

// Timings: build is the C# loop; render is from the switch to the component's next OnAfterRender, i.e. the server side of
// the first render after the swap, kept per mode. Browser layout and paint are not included.
private readonly Stopwatch _swap = new();

private bool _swapPending;

private double _buildMs;

private readonly Dictionary<string, double> _renderMs = new();

private int _editCount, _page;

private string Summary
{
    get
    {
        string Ms(string key) => _renderMs.TryGetValue(key, out var ms) ? $"{ms:0} ms" : "not yet";
        var page = _paging && _page > 0 ? $" Page {_page}." : "";
        return $"{_rows.Count:N0} rows built in {_buildMs:0.0} ms. First render after the switch, on the server: "
             + $"scroll {Ms("scroll")} · 25 per page {Ms("page25")} · 100 per page {Ms("page100")}. "
             + $"{_editCount} cell{(_editCount == 1 ? "" : "s")} saved.{page}";
    }
}

protected override void OnInitialized()
{
    _events.OnCellSave = EventCallback.Factory.Create<CellSaveArgs<VRow>>(this, _ => { _editCount++; StateHasChanged(); });
    _events.PageChanged = EventCallback.Factory.Create<PageChangeEventArgs>(this, e => { _page = e.CurrentPage; StateHasChanged(); });

    var sw = Stopwatch.StartNew();
    var categories = new[] { "Hotel", "Flight", "Tour", "Transfer", "Cruise", "Rail" };
    var rand = new Random(101);
    var list = new List<VRow>(1000);
    for (var i = 1; i <= 1000; i++)
    {
        var cat = categories[i % categories.Length];
        list.Add(new VRow
        {
            Code = $"V-{i:D4}", Description = $"{cat} service #{i}", Category = cat,
            Qty = rand.Next(1, 150), Price = Math.Round((decimal)(rand.NextDouble() * 320 + 8.5), 2), Available = i % 3 != 0,
        });
    }
    _rows = list;
    _buildMs = sw.Elapsed.TotalMilliseconds;
    Remount();
}

protected override void OnAfterRender(bool firstRender)
{
    if (!_swapPending) return;
    _swapPending = false;
    _renderMs[_modeKey] = _swap.Elapsed.TotalMilliseconds;
    _ = InvokeAsync(StateHasChanged);   // show the number; this render has no pending swap, so it ends here
}

private void PickMode(IReadOnlyList<string> values)
{
    if (values.Count == 0 || values[0] == _modeKey) return;
    _modeKey = values[0];
    _paging = _modeKey != "scroll";
    _pageSize = _modeKey == "page100" ? 100 : 25;
    _page = 0;
    Remount();
}

private void Remount()
{
    _mountGen++;
    _swap.Restart();
    _swapPending = true;
}
```

