From 4414e9f842fba3763bc36ae3869fc00e9781d1ba Mon Sep 17 00:00:00 2001 From: Dimo Dimov <961014+dimodi@users.noreply.github.com> Date: Mon, 13 Jan 2025 09:50:13 +0200 Subject: [PATCH 1/2] docs(common): Enhance common Rebind() example --- common-features/data-binding/overview.md | 46 +++++++++++++----------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/common-features/data-binding/overview.md b/common-features/data-binding/overview.md index d246f681c4..7f4c4048b3 100644 --- a/common-features/data-binding/overview.md +++ b/common-features/data-binding/overview.md @@ -115,31 +115,36 @@ Thus, you will usually need to create a new reference for `Data` value in order >caption Call `Rebind()` or create new Data reference ````RAZOR -

- Refresh Grid Data -

- + AutoGenerateColumns="true"> + + Modify Grid Data And Refresh + + @code { - TelerikGrid GridRef { get; set; } - List GridData { get; set; } + private TelerikGrid? GridRef { get; set; } + + private List GridData { get; set; } = new(); + + private int LastId { get; set; } - void RefreshGridData() + private void RefreshGridData() { - var newId = GridData.Count + 1; + GridData.RemoveAt(0); - GridData.FirstOrDefault().Text = DateTime.Now.Ticks.ToString(); + GridData.ElementAt(Random.Shared.Next(0, GridData.Count)).Text = DateTime.Now.Ticks.ToString(); - GridData.Add(new SampleModel() { - Id = newId, - Text = "Text " + newId + GridData.Add(new SampleModel() + { + Id = ++LastId, + Text = "Text " + LastId }); // Call Rebind... - GridRef.Rebind(); + GridRef?.Rebind(); // ...OR... @@ -152,13 +157,12 @@ Thus, you will usually need to create a new reference for `Data` value in order protected override void OnInitialized() { - GridData = new List(); - - for (int i = 1; i <= 3; i++) + for (int i = 1; i <= 5; i++) { - GridData.Add(new SampleModel() { - Id = i, - Text = "Text " + i + GridData.Add(new SampleModel() + { + Id = ++LastId, + Text = $"Text {LastId}" }); } @@ -168,7 +172,7 @@ Thus, you will usually need to create a new reference for `Data` value in order public class SampleModel { public int Id { get; set; } - public string Text { get; set; } + public string Text { get; set; } = string.Empty; } } ```` From ce30dd72b38ba6f6828f7f7ec972e8accc8a5bf7 Mon Sep 17 00:00:00 2001 From: Dimo Dimov <961014+dimodi@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:00:01 +0200 Subject: [PATCH 2/2] Update onread.md --- common-features/data-binding/onread.md | 51 +++++++++++++------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/common-features/data-binding/onread.md b/common-features/data-binding/onread.md index c34af710b9..f49bd2d92b 100644 --- a/common-features/data-binding/onread.md +++ b/common-features/data-binding/onread.md @@ -220,7 +220,13 @@ Also check [how to rebind and refresh a component with a `Timer`](slug://common- ````RAZOR @using Telerik.DataSource.Extensions -Rebind Components + +
+
+ + -Rebind Components -

- @code { - TelerikGrid TheGrid { get; set; } - TelerikDropDownList TheDropDown { get; set; } + private TelerikGrid? GridRef { get; set; } + private TelerikDropDownList? DropDownListRef { get; set; } - List GridData { get; set; } - List DropDownData { get; set; } + private List GridData { get; set; } = new(); + private List DropDownData { get; set; } = new(); - int DropDownValue { get; set; } = 1; + private int DropDownValue { get; set; } = 1; - int ItemCounter { get; set; } = 3; + private int ItemCounter { get; set; } = 3; - void RebindComponents() + private void RebindComponents() { - GenerateData(); // simulate change in the data + GenerateData(); // simulate data change - TheGrid.Rebind(); - TheDropDown.Rebind(); + GridRef?.Rebind(); + DropDownListRef?.Rebind(); } - async Task OnGridRead(GridReadEventArgs args) + private async Task OnGridRead(GridReadEventArgs args) { - var result = GridData.ToDataSourceResult(args.Request); + var result = await GridData.ToDataSourceResultAsync(args.Request); args.Data = result.Data; args.Total = result.Total; } - async Task OnDropDownRead(DropDownListReadEventArgs args) + private async Task OnDropDownRead(DropDownListReadEventArgs args) { - var result = DropDownData.ToDataSourceResult(args.Request); + var result = await DropDownData.ToDataSourceResultAsync(args.Request); args.Data = result.Data; args.Total = result.Total; } @@ -290,17 +293,15 @@ Also check [how to rebind and refresh a component with a `Timer`](slug://common- base.OnInitialized(); } - void GenerateData() + private void GenerateData() { GridData = new List(); DropDownData = new List(); - var rnd = new Random(); - for (int i = 1; i <= ItemCounter; i++) { - GridData.Add(new SampleModel() { Id = i, Text = $"Text {rnd.Next(1, 100)}" }); - DropDownData.Add(new SampleModel() { Id = i, Text = $"Text {rnd.Next(1, 100)}" }); + GridData.Add(new SampleModel() { Id = i, Text = $"Text {(char)Random.Shared.Next(65, 91)}{(char)Random.Shared.Next(65, 91)}" }); + DropDownData.Add(new SampleModel() { Id = i, Text = $"Text {(char)Random.Shared.Next(65, 91)}{(char)Random.Shared.Next(65, 91)}" }); } ItemCounter++; @@ -309,7 +310,7 @@ Also check [how to rebind and refresh a component with a `Timer`](slug://common- public class SampleModel { public int Id { get; set; } - public string Text { get; set; } + public string Text { get; set; } = string.Empty; } } ````