Your search did not match any results.

This example demonstrates real-time data update in the DataGrid widget bound to a SignalR server. Note that data used in this demo is for demonstration purposes only.

@using DevExtreme.NETCore.Demos.Models.SignalR @(Html.DevExtreme().DataGrid<Stock>() .ID("gridContainer") .LoadPanel(loadPanel => loadPanel.Enabled(false)) .Columns(columns => { columns.AddFor(m => m.LastUpdate).DataField("lastUpdate") .Width(110) .Format(Format.LongTime); columns.AddFor(m => m.Symbol).DataField("symbol"); columns.AddFor(m => m.Price).DataField("price") .Format("#0.####") .CellTemplate(@<text> <span class="<%= data.change > 0 ? 'inc' : 'dec' %>"> <%= text %> </span> </text>); columns.AddFor(m => m.Change).DataField("change") .Width(140) .Format("#0.####") .CellTemplate(@<text> <span class="<%= data.change > 0 ? 'inc' : 'dec' %>"> <span class="current-value"><%= text %></span> <span class="arrow"></span> <span class="diff"><%= data.percentChange.toFixed(2) %>%</span> </span> </text>); columns.AddFor(m => m.DayOpen).DataField("dayOpen") .Format("#0.####"); columns.AddFor(m => m.DayMin).DataField("dayMin") .Format("#0.####"); columns.AddFor(m => m.DayMax).DataField("dayMax") .Format("#0.####"); }) ) <script src="~/signalr/signalr-client.js"></script> <script> var connection = new signalR.HubConnection("@Url.Content("~/liveUpdateSignalRHub")"); $(function () { var arrayStore, dataSource; connection.on("updateStockPrice", function (data) { if (arrayStore) { arrayStore.update(data.symbol, data); dataSource.reload(); } }); connection.start() .then(function () { connection.invoke("getAllStocks").then(function (data) { arrayStore = new DevExpress.data.ArrayStore({ key: "symbol", data: data }); dataSource = new DevExpress.data.DataSource({ store: arrayStore }); $("#gridContainer").dxDataGrid({ dataSource: dataSource }); }); }); }); </script>
using DevExtreme.NETCore.Demos.Models; using DevExtreme.NETCore.Demos.Models.DataGrid; using DevExtreme.NETCore.Demos.Models.SampleData; using Microsoft.AspNetCore.Mvc; using System.Linq; namespace DevExtreme.NETCore.Demos.Controllers { public class DataGridController : Controller { public ActionResult SignalRService() { return View(); } } }
using System; using System.Collections.Generic; using DevExtreme.NETCore.Demos.Models.SignalR; using Microsoft.AspNetCore.SignalR; namespace DevExtreme.NETCore.Demos.Hubs { public class LiveUpdateSignalRHub : Hub { private readonly StockTicker _stockTicker; public LiveUpdateSignalRHub(StockTicker stockTicker) { _stockTicker = stockTicker; } public IEnumerable<Stock> GetAllStocks() { return _stockTicker.GetAllStocks(); } } }
using System; namespace DevExtreme.NETCore.Demos.Models.SignalR { public class Stock { public Stock(decimal price) { Price = price; DayMax = price; DayMin = price; _initPrice = Price; } public string Symbol { get; set; } public decimal Price { get; set; } public decimal DayMax { get; set; } public decimal DayMin { get; set; } public decimal DayOpen { get; set; } public DateTime LastUpdate { get; set; } public decimal Change { get { return Price - DayOpen; } } public double PercentChange { get { return (double)Math.Round(Change * 100 / DayOpen, 2); } } decimal _initPrice; public void Update() { bool isNewDay = LastUpdate.Day != DateTime.Now.Day; decimal change = GenerateChange(); decimal newPrice = _initPrice + _initPrice * change; Price = newPrice; LastUpdate = DateTime.Now; if(Price > DayMax || isNewDay) DayMax = Price; if(Price < DayMin || isNewDay) DayMin = Price; } static Random random = new Random(); decimal GenerateChange() { return (decimal)random.Next(-200, 200) / 10000; } } }
using System; using System.Collections.Generic; using System.Threading; using DevExtreme.NETCore.Demos.Hubs; using Microsoft.AspNetCore.SignalR; namespace DevExtreme.NETCore.Demos.Models.SignalR { public class StockTicker { private readonly List<Stock> _stocks = new List<Stock>(); private IHubContext<LiveUpdateSignalRHub> _hubContext { get; set; } private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(2000); private readonly Random _updateOrNotRandom = new Random(); private readonly Timer _timer; private readonly object _updateStockPricesLock = new object(); static readonly Random random = new Random(); public StockTicker(IHubContext<LiveUpdateSignalRHub> hubContext) { _hubContext = hubContext; _stocks = GenerateStocks(); _timer = new Timer(UpdateStockPrices, null, _updateInterval, _updateInterval); } public List<Stock> GetAllStocks() { return _stocks; } static List<Stock> GenerateStocks() { return new List<Stock> { new Stock(37.95M) { Symbol = "MSFT", DayOpen=36.5M, LastUpdate = DateTime.Now }, new Stock(24.85M) { Symbol = "INTC", DayOpen=24.9M, LastUpdate = DateTime.Now }, new Stock(22.99M){ Symbol = "CSCO", DayOpen=22.7M, LastUpdate = DateTime.Now }, new Stock(30.71M){ Symbol = "SIRI", DayOpen=30.7M, LastUpdate = DateTime.Now }, new Stock(58.73M){ Symbol = "AAPL", DayOpen=54.9M, LastUpdate = DateTime.Now }, new Stock(110M){ Symbol = "HOKU", DayOpen=121.2M, LastUpdate = DateTime.Now }, new Stock(38.11M){ Symbol = "ORCL", DayOpen=37.9M, LastUpdate = DateTime.Now }, new Stock(17.61M) { Symbol = "AMAT", DayOpen=17.5M, LastUpdate = DateTime.Now }, new Stock(40.80M){ Symbol = "YHOO", DayOpen=39.9M, LastUpdate = DateTime.Now }, new Stock(31.85M){ Symbol = "LVLT", DayOpen=32.9M, LastUpdate = DateTime.Now }, new Stock(20.63M){ Symbol = "DELL", DayOpen=17.9M, LastUpdate = DateTime.Now }, new Stock(63.70M) { Symbol = "GOOG", DayOpen=55.9M, LastUpdate = DateTime.Now } }; } private void UpdateStockPrices(object state) { lock(_updateStockPricesLock) { foreach(var stock in _stocks) { if(TryUpdateStockPrice(stock)) { BroadcastStockPrice(stock); } } } } private bool TryUpdateStockPrice(Stock stock) { var r = _updateOrNotRandom.NextDouble(); if(r > .1) { return false; } stock.Update(); return true; } private void BroadcastStockPrice(Stock stock) { _hubContext.Clients.All.InvokeAsync("updateStockPrice", stock); } } }
#gridContainer span.current-value { display: inline-block; margin-right: 5px; } #gridContainer span.diff { width: 50px; display: inline-block; } #gridContainer .inc { color: #2ab71b; } #gridContainer .dec { color: #f00; } #gridContainer .inc .arrow, #gridContainer .dec .arrow { display: inline-block; height: 10px; width: 10px; background-repeat: no-repeat; background-size: 10px 10px; } #gridContainer .inc .arrow { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADKSURBVHjaYtTaLs1ABEiG0nPRJa56PEHhsxBhmCUQT4OyrwHxcXyKmQgYJgHE64CYDYrXQcXIMhCbAcgWkGzgNKh38QUB0QamIUUErkhKI9ZAGyCeTERkTYaqxWsgKA2txhdG6GGsvUNGGpeBRMUiGhCFGsqGzUBQQJsxkA5AemaiG5hDIBIIgQSgK0FmMDACs549kN5FZLjhA7+A2A2U9YSAOBeLAk4gnoBDczoOcSFGPIUDPxB/wCHHiKtwYGKgMhg1cBAaCBBgAJTUIL3ToPZfAAAAAElFTkSuQmCC'); } #gridContainer .dec .arrow { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADJSURBVHjaYvzPgBfgkhYA4o8QFahKmBioDEYNHIQGsgBxIBCLkqgvAYi/g1mMjMjir0EJzR6If/6HpChKMMgMe3DKBeIcKhiY8x/MYoDj+RQYNgdkGLqBbEB8kgzDToL1YjEQhKWB+BUJhj0H64Eahs1AELYhMpJ+gtUiGYbLQBBOI8LANLBaIg1kAAc0vkiAqSPBQFAkHcNi2DGoHMkGgrAENOCRI0ECRQ2JBoKwJTQCfkLZDPgMZPxPXN5NhtJzMSsJVBMAAgwAyWSY2svfmrwAAAAASUVORK5CYII='); }