# Blazor Grid with 100,000 Rows

URL: https://flexcoreui.com/demo/grid/row-selection-50k  
Section: Data Grid  
Controls: GridControl, GridColumn  
Keywords: virtualization, virtual scrolling, large data, 100k rows, row windowing, performance

Scroll 1,000 to 100,000 rows in a Blazor grid that renders only the rows in view, with the server time of each run measured.

## Documentation

### Grid

| Name | Type | Default | Description |
|---|---|---|---|
| WindowOverscanRows | int | 60 | Rows rendered beyond the visible slice on each side, so a fast flick does not outrun the render. The ceiling when the cell budget is active. |
| WindowOverscanCellBudget | int | 900 | Per-side overscan budget in cells (rows × visible columns). The effective overscan is the budget divided by the visible columns, clamped between min(15, WindowOverscanRows) and WindowOverscanRows. 0 turns the budget off, as on this page. |
| EnableGroupedRowWindowing | bool | true | Windows grouped grids too. Off renders every grouped row. |
| MinimumRowsForWindowing | int | 0 | Source row count below which the grid does not window. 0 windows at any size. |
| AllowGrouping | bool | false | Lets the user group by a column; the group drop area appears above the header. |
| GroupColumns | List<string>? | null | Initial group columns, by field name. |
| ShowGroupExpandCollapse | bool | true | Expand all / collapse all toggle in the group drop area. |
| EnableTypeSearch | bool | false | With the grid focused, printable keys search the active column for the first row whose text starts with the typed buffer. |
| 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. |
| SelectionSettingsRef | SelectionSettings? | null | Selection options object; see the next table. |
| EditSettingsRef | EditSettings? | null | Editing options object. Batch mode here, so edits are kept until the batch is saved. |
| EventsRef | GridControlEvents<TValue>? | null | Event callbacks object; two of them are wired here. |

### Selection

| Name | Type | Default | Description |
|---|---|---|---|
| Type | SelectionType | Single | Single or Multiple rows. |
| Mode | SelectionMode | Row | Row or Cell selection. |

### Events

| Name | Type | Description |
|---|---|---|
| SelectionChanged | int | After the selection changes, with the number of selected rows. |
| OnCellSave | CellSaveArgs<TValue> | After a cell's new value has been written to its row: a real change, or an editor the user touched. Carries the row, the column field and the value. |

## Examples

### Rows

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

<ButtonGroupControl Items="_rowCountItems" Values="@(new[] { _rowCount.ToString() })" ValuesChanged="PickRowCount" Selection="ChoiceSelectionMode.Single" AriaLabel="Row count" />
<ButtonGroupControl Items="_overscanItems" Values="@(new[] { _overscan.ToString() })" ValuesChanged="PickOverscan" Selection="ChoiceSelectionMode.Single" AriaLabel="Overscan rows" />
<ToggleButtonControl Selected="@(_groupColumns.Count > 0)" SelectedChanged="SetGrouped">Group by Category</ToggleButtonControl>
<ToggleButtonControl Selected="@_groupedWindowing" SelectedChanged="SetGroupedWindowing">Window grouped rows</ToggleButtonControl>

<GridControl @key="@_mountGen"
             TValue="SRow"
             DataSource="@_rows"
             Height="480px"
             WindowOverscanRows="@_overscan"
             WindowOverscanCellBudget="0"
             AllowGrouping="true"
             GroupColumns="@_groupColumns"
             EnableGroupedRowWindowing="@_groupedWindowing"
             ShowGroupExpandCollapse="true"
             AllowSorting="true"
             AllowFiltering="true"
             AllowResizing="true"
             AllowColumnReorder="true"
             EnableTypeSearch="true"
             ShowRowSelectionEditColumnCue="true"
             ShowRowCountInFooter="true"
             SelectionSettingsRef="@_selection"
             EditSettingsRef="@_edit"
             EventsRef="@_events">
    <GridColumnsBase>
        <GridColumn Field="@nameof(SRow.Item)" HeaderText="Item Code" Width="100px" AllowEditing="false" />
        <GridColumn Field="@nameof(SRow.Description)" HeaderText="Description" Width="220px" />
        <GridColumn Field="@nameof(SRow.Category)" HeaderText="Category" Width="130px" />
        <GridColumn Field="@nameof(SRow.Qty)" HeaderText="Qty" Width="85px" Type="ColumnType.Number" TextAlign="TextAlign.Right" SelectAllOnEdit="true" />
        <GridColumn Field="@nameof(SRow.UnitCost)" HeaderText="Unit Cost" Width="95px" Type="ColumnType.Number" Format="N2" TextAlign="TextAlign.Right" SelectAllOnEdit="true" />
        <GridColumn Field="@nameof(SRow.Active)" HeaderText="Active" Width="75px" Type="ColumnType.Boolean" TextAlign="TextAlign.Center" />
        <GridColumn Field="@nameof(SRow.Season)" HeaderText="Season" Width="100px" AllowEditing="false" />
        <GridColumn Field="@nameof(SRow.Currency)" HeaderText="Currency" Width="100px" AllowEditing="false" />
        <GridColumn Field="@nameof(SRow.Board)" HeaderText="Board" Width="100px" AllowEditing="false" />
        <GridColumn Field="@nameof(SRow.Desk)" HeaderText="Desk" Width="100px" AllowEditing="false" />
        <GridColumn Field="@nameof(SRow.Supplier)" HeaderText="Supplier" Width="120px" AllowEditing="false" />
        <GridColumn Field="@nameof(SRow.Ref)" HeaderText="Booking ref" Width="100px" AllowEditing="false" />
    </GridColumnsBase>
</GridControl>
<p class="result">@Summary</p>
```

```csharp
public class SRow
{
    public string Item { get; set; } = "";
    public string Description { get; set; } = "";
    public string Category { get; set; } = "";
    public decimal Qty { get; set; }
    public decimal UnitCost { get; set; }
    public bool Active { get; set; }
    public string Season { get; set; } = "";
    public string Currency { get; set; } = "";
    public string Board { get; set; } = "";
    public string Desk { get; set; } = "";
    public string Supplier { get; set; } = "";
    public string Ref { get; set; } = "";
}

private readonly SelectionItem[] _rowCountItems = { new("1000", "1,000"), new("5000", "5,000"), new("20000", "20,000"), new("50000", "50,000"), new("100000", "100,000") };

private readonly SelectionItem[] _overscanItems = { new("15", "15 rows"), new("60", "60 rows"), new("260", "260 rows") };

private List<SRow> _rows = [];

private int _rowCount = 5000;

private int _overscan = 60;

private List<string> _groupColumns = ["Category"];

private bool _groupedWindowing = true;

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<SRow> _events = new();

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

private bool _swapPending;

private double _buildMs, _renderMs;

private int _selectedCount, _editCount;

private string Summary =>
    $"{_rowCount:N0} rows built in {_buildMs:0.0} ms; first render after the swap took {_renderMs:0} ms on the server; " +
    $"{_selectedCount} selected; {_editCount} edit{(_editCount == 1 ? "" : "s")} saved.";

protected override void OnInitialized()
{
    _events.SelectionChanged = EventCallback.Factory.Create<int>(this, count => { _selectedCount = count; StateHasChanged(); });
    _events.OnCellSave = EventCallback.Factory.Create<CellSaveArgs<SRow>>(this, _ => { _editCount++; StateHasChanged(); });
    Rebuild();
}

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

private void Rebuild()
{
    var sw = Stopwatch.StartNew();
    var categories = new[] { "Flights", "Hotels", "Transfers", "Excursions", "Cruises", "Rail", "Insurance", "Car rental" };
    var seasons = new[] { "Summer", "Fall", "Winter", "Spring" };
    var currencies = new[] { "EUR", "USD", "GBP" };
    var boards = new[] { "Room only", "Bed and breakfast", "Half board", "All inclusive" };
    var desks = new[] { "Boston", "Lisbon", "Kyoto", "Cape Town", "Manila" };
    var rand = new Random(77);
    var list = new List<SRow>(_rowCount);
    for (var i = 1; i <= _rowCount; i++)
    {
        var cat = categories[i % categories.Length];
        list.Add(new SRow
        {
            Item = $"ITM-{i:D6}", Description = $"{cat} rate line #{i}", Category = cat,
            Qty = rand.Next(1, 100), UnitCost = Math.Round((decimal)(rand.NextDouble() * 180 + 12), 2), Active = i % 5 != 0,
            Season = seasons[i % 4], Currency = currencies[i % 3], Board = boards[i % 4], Desk = desks[i % 5], Supplier = $"Supplier {i % 97 + 1:D2}", Ref = $"RSG-{i:D6}",
        });
    }
    _rows = list;
    _buildMs = sw.Elapsed.TotalMilliseconds;
    Remount();
}

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

private void PickRowCount(IReadOnlyList<string> values)
{
    if (values.Count == 0) return;
    _rowCount = int.Parse(values[0]);
    Rebuild();
}

private void PickOverscan(IReadOnlyList<string> values)
{
    if (values.Count == 0) return;
    _overscan = int.Parse(values[0]);
    Remount();
}

private void SetGrouped(bool on) { _groupColumns = on ? ["Category"] : []; Remount(); }

private void SetGroupedWindowing(bool on) { _groupedWindowing = on; Remount(); }
```

