# Real-Time Blazor Grid with Live Updates

URL: https://flexcoreui.com/demo/grid/live-updates  
Section: Data Grid  
Controls: GridControl, GridColumn  
Keywords: real-time grid, live data, observable data, high-frequency updates, cell template

Keep a Blazor grid in step with live data: rows change several times a second while users sort, with templated cells and a sparkline column.

## Documentation

### Column

| Name | Type | Default | Description |
|---|---|---|---|
| Field | string |  | Property the column reads, and sorts by. |
| HeaderText | string? |  | Header caption. |
| Template | RenderFragment<object>? |  | Markup for the cell, given the row object as context; used here for the price tint, the change arrows, the trend line and the profit color. |
| Type | ColumnType |  | Text, Number, Date or Boolean: decides editor, alignment default and sorting. |
| Format | string? |  | Format string for the cell text when there is no template. |
| TextAlign | TextAlign? | null | Left, Right or Center; the type's default when null. |
| Width | string? |  | CSS width of the column. |
| AllowSorting | bool |  | Off on the trend column, whose field only repeats the ticker. |

## Examples

### Ticker

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

<ButtonControl CssClass="@(_streaming ? "btn-sm btn-danger" : "btn-sm btn-success")" OnClick="ToggleStreaming">@(_streaming ? "Pause" : "Start")</ButtonControl>
<ButtonGroupControl Items="_intervalItems" Values="@(new[] { _intervalMs.ToString() })" ValuesChanged="PickInterval" Selection="ChoiceSelectionMode.Single" AriaLabel="Tick interval" />
<ButtonControl CssClass="btn-sm" OnClick="Tick">Tick once</ButtonControl>
<ButtonControl CssClass="btn-sm" OnClick="Reset">Reset</ButtonControl>

<GridControl TValue="TickerRow"
             DataSource="@_rows"
             Height="420px"
             AllowSorting="true"
             AllowSelection="true"
             AllowResizing="true"
             ShowRowCountInFooter="true">
    <GridColumnsBase>
        <GridColumn Field="@nameof(TickerRow.Ticker)" HeaderText="Ticker" Width="90px">
            <Template>
                @{ var row = (TickerRow)context; }
                <strong class="ticker">@row.Ticker</strong>
            </Template>
        </GridColumn>
        <GridColumn Field="@nameof(TickerRow.Name)" HeaderText="Name" Width="170px" />
        <GridColumn Field="@nameof(TickerRow.Instrument)" HeaderText="Type" Width="80px" />
        <GridColumn Field="@nameof(TickerRow.Price)" HeaderText="Price" Width="110px" Type="ColumnType.Number" TextAlign="TextAlign.Right">
            <Template>
                @{ var row = (TickerRow)context; }
                <span class="price @(row.TickDirection > 0 ? "up" : row.TickDirection < 0 ? "down" : "")">@row.Price.ToString("C2")</span>
            </Template>
        </GridColumn>
        <GridColumn Field="@nameof(TickerRow.Delta)" HeaderText="Change" Width="105px" Type="ColumnType.Number" TextAlign="TextAlign.Right">
            <Template>
                @{ var row = (TickerRow)context; }
                <span class="@(row.Delta >= 0 ? "gain" : "loss")">@(row.Delta > 0 ? "▲ +" : row.Delta < 0 ? "▼ " : "")@row.Delta.ToString("C2")</span>
            </Template>
        </GridColumn>
        <GridColumn Field="@nameof(TickerRow.DeltaPercent)" HeaderText="Change %" Width="95px" Type="ColumnType.Number" TextAlign="TextAlign.Right">
            <Template>
                @{ var row = (TickerRow)context; }
                <span class="@(row.DeltaPercent >= 0 ? "gain" : "loss")">@(row.DeltaPercent > 0 ? "+" : "")@row.DeltaPercent.ToString("0.00")%</span>
            </Template>
        </GridColumn>
        <GridColumn Field="@nameof(TickerRow.Ticker)" HeaderText="Trend" Width="130px" AllowSorting="false" TextAlign="TextAlign.Center">
            <Template>
                @{ var row = (TickerRow)context; }
                <svg class="spark" viewBox="0 0 110 24" aria-hidden="true"><polyline points="@Sparkline(row.Timeline)" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" /></svg>
            </Template>
        </GridColumn>
        <GridColumn Field="@nameof(TickerRow.Quantity)" HeaderText="Holdings" Width="85px" Type="ColumnType.Number" Format="N0" TextAlign="TextAlign.Right" />
        <GridColumn Field="@nameof(TickerRow.TotalValue)" HeaderText="Market value" Width="120px" Type="ColumnType.Number" Format="C2" TextAlign="TextAlign.Right" />
        <GridColumn Field="@nameof(TickerRow.UnrealizedPL)" HeaderText="Unrealized P&amp;L" Width="125px" Type="ColumnType.Number" TextAlign="TextAlign.Right">
            <Template>
                @{ var row = (TickerRow)context; }
                <span class="@(row.UnrealizedPL >= 0 ? "gain" : "loss")">@(row.UnrealizedPL > 0 ? "+" : "")@row.UnrealizedPL.ToString("C2")</span>
            </Template>
        </GridColumn>
        <GridColumn Field="@nameof(TickerRow.LastUpdated)" HeaderText="Updated" Width="100px" Type="ColumnType.Date" Format="HH:mm:ss" TextAlign="TextAlign.Center" />
    </GridColumnsBase>
</GridControl>
<p class="result">@Status</p>
```

```csharp
private readonly SelectionItem[] _intervalItems = { new("100", "100 ms"), new("250", "250 ms"), new("500", "500 ms"), new("1000", "1 s") };

private List<TickerRow> _rows = Load();

private System.Timers.Timer? _timer;

private bool _streaming;

private int _intervalMs = 500;

// Measured per tick: rows changed, and the server render time from the tick to the component's next OnAfterRender.
private readonly Stopwatch _tickWatch = new();

private readonly Queue<double> _recent = new();

private bool _renderPending;

private int _ticks, _lastChanged;

private double _lastMs;

private string Status
{
    get
    {
        var avg = _recent.Count == 0 ? 0 : _recent.Average();
        var state = _streaming ? $"Streaming every {_intervalMs} ms" : "Paused";
        return _ticks == 0
            ? $"{state}. {_rows.Count} instruments, no ticks yet."
            : $"{state}. {_ticks} ticks; the last changed {_lastChanged} rows and rendered in {_lastMs:0.0} ms on the server (average {avg:0.0} ms over the last {_recent.Count}).";
    }
}

private void ToggleStreaming()
{
    if (_streaming) { _timer?.Stop(); _streaming = false; return; }
    _streaming = true;
    StartTimer();
}

private void PickInterval(IReadOnlyList<string> values)
{
    if (values.Count == 0) return;
    _intervalMs = int.Parse(values[0]);
    if (_streaming) StartTimer();
}

private void StartTimer()
{
    _timer?.Stop();
    _timer?.Dispose();
    _timer = new System.Timers.Timer(_intervalMs) { AutoReset = true };
    _timer.Elapsed += async (_, _) => await InvokeAsync(Tick);   // back on the circuit before touching the rows
    _timer.Start();
}

// One tick: three to seven instruments move by up to 1.5 percent; the rows are changed in place and the page re-renders.
private void Tick()
{
    if (_rows.Count == 0) return;
    _tickWatch.Restart();
    var count = Random.Shared.Next(3, 8);
    for (var i = 0; i < count; i++)
    {
        var row = _rows[Random.Shared.Next(_rows.Count)];
        row.PreviousPrice = row.Price;
        var change = Math.Round(row.Price * (decimal)((Random.Shared.NextDouble() - 0.49) * 0.03), 2);
        if (change == 0) change = 0.05m;
        row.Price = Math.Max(0.50m, row.Price + change);
        row.TickDirection = row.Price > row.PreviousPrice ? 1 : -1;
        row.LastUpdated = DateTime.Now;
        row.Timeline.Add((double)row.Price);
        if (row.Timeline.Count > 24) row.Timeline.RemoveAt(0);
    }
    _lastChanged = count;
    _ticks++;
    _renderPending = true;
    StateHasChanged();
}

protected override void OnAfterRender(bool firstRender)
{
    if (!_renderPending) return;
    _renderPending = false;
    _lastMs = _tickWatch.Elapsed.TotalMilliseconds;
    _recent.Enqueue(_lastMs);
    while (_recent.Count > 20) _recent.Dequeue();
    _ = InvokeAsync(StateHasChanged);   // show the number; this render has no pending tick, so it ends here
}

private void Reset()
{
    _rows = Load();
    _ticks = 0; _lastChanged = 0; _lastMs = 0; _recent.Clear();
}

// Seeded portfolio; no instrument has moved yet, so no price cell is tinted until the first tick.
private static List<TickerRow> Load()
{
    var rows = BenchDataStore.GetFinanceTickers();
    foreach (var row in rows) { row.TickDirection = 0; row.PreviousPrice = row.Price; }
    return rows;
}

private static string Sparkline(List<double> points)
{
    if (points.Count < 2) return "0,12 110,12";
    var min = points.Min(); var range = Math.Max(points.Max() - min, 0.001);
    return string.Join(" ", points.Select((p, i) => $"{5 + i * 100.0 / (points.Count - 1):F1},{22 - (p - min) / range * 16:F1}"));
}

public void Dispose()
{
    _timer?.Stop();
    _timer?.Dispose();
}
```

