ComboBox - Refresh Data
The most common reason you would use an ObservableCollection is to make a component (like a grid, treeview, treelist, dropdown) change or react when you change that collection.
When you want to refresh the component data source like that, there are two important framework behaviors you need to be aware of - when ObservableCollection instances fire events, and how to refresh the data of a component when it is not an observable collection.
In this article:
Rebind Method
To refresh the ComboBox data when using OnRead
, call the Rebind
method of the TelerikComboBox reference. This will fire the OnRead
event and execute the business logic in the handler.
@* Clicking on the Rebind button will delete the first option from the dropdown and refresh the data *@
@using Telerik.DataSource.Extensions
<TelerikButton OnClick="@RebindComboBox">Rebind</TelerikButton>
<TelerikComboBox @ref="@ComboBoxRef"
TItem="Product" TValue="int"
OnRead="@ReadItems"
ValueField="@nameof(Product.ProductId)"
TextField="@nameof(Product.ProductName)"
Filterable="true"
Placeholder="Find what you seek by typing"
@bind-Value="@SelectedValue">
</TelerikComboBox>
@code{
public int SelectedValue { get; set; }
List<Product> AllData { get; set; } = new List<Product>();
public TelerikComboBox<Product, int> ComboBoxRef { get; set; }
async Task ReadItems(ComboBoxReadEventArgs args)
{
await Task.Delay(1000);
args.Data = AllData.ToDataSourceResult(args.Request).Data;
}
protected override void OnInitialized()
{
List<Product> products = new List<Product>();
for (int i = 0; i < 200; i++)
{
products.Add(new Product()
{
ProductId = i,
ProductName = "Product" + i.ToString(),
SupplierId = i,
UnitPrice = (decimal)(i * 3.14),
UnitsInStock = (short)(i * 1),
});
}
AllData = products;
}
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int SupplierId { get; set; }
public decimal UnitPrice { get; set; }
public short UnitsInStock { get; set; }
}
private void RebindComboBox()
{
if (AllData.Count > 0)
{
AllData.RemoveAt(0);
}
ComboBoxRef.Rebind();
}
}
As part of our
3.0.1
release we introduced theRebind
method to the component reference. This would make the rest of the approaches in this article obsolete.
Observable Data
Databound components can benefit from live updates - when the data source collection changes, the components should update to reflect that change. Most data-bound components in the Telerik UI for Blazor suite implement such functionality.
When the Data
of the component is a collection that implements the INotifyCollectionChanged
interface (such as ObservableCollection
), the Telerik components subscribe to its CollectionChanged
event to make live update. This means that adding items, removing items, or clearing the collection updates the components (its .Add()
, .Remove()
and .Clear()
methods).
The Observable collections fire the CollectionChanged
event only when their Add
, Remove
and Clear
methods are called. They do not fire it when you change the value of a field of one of their elements.
@* Add/remove an option to see how the ComboBox reacts to the change. *@
@using System.Collections.ObjectModel
<h4>Add option</h4>
<TelerikTextBox @bind-Value="@ValuetoAdd"></TelerikTextBox>
<TelerikButton OnClick="@AddOption">Add option</TelerikButton>
<br />
<h4>Remove the last option</h4>
<TelerikButton OnClick="@RemoveOption">Remove the last option</TelerikButton>
<br />
<h4>ComboBox options: @myDdlData.Count</h4>
<br />
<TelerikComboBox Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" @bind-Value="@selectedValue">
</TelerikComboBox>
@code {
string ValuetoAdd { get; set; }
int selectedValue { get; set; }
ObservableCollection<MyDdlModel> myDdlData = new ObservableCollection<MyDdlModel>(Enumerable.Range(1, 5).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x }));
void AddOption()
{
if (!string.IsNullOrWhiteSpace(ValuetoAdd))
{
myDdlData.Add(
new MyDdlModel { MyTextField = ValuetoAdd, MyValueField = myDdlData.Count + 1 }
);
ValuetoAdd = string.Empty;
}
}
void RemoveOption()
{
if (myDdlData.Count > 0)
{
myDdlData.RemoveAt(myDdlData.Count - 1);
}
}
public class MyDdlModel
{
public int MyValueField { get; set; }
public string MyTextField { get; set; }
}
}
If you need to add/remove many items to/from the collection, consider creating a new collection and provide its reference to the data parameter. Thus, the component will re-render only once (when the data collection reference is changed) instead of re-rendering multiple times in response to the Add/Remove events.
New Collection Reference
In Blazor, the framework will fire the OnParametersSet
event of a child component (which is how child components can react to outside changes) only when it can detect a change in the object it receives through the corresponding parameter (like Data
for the data sources of Telerik components). This detection works as follows:
For strings and value types, this happens when their value changes.
-
For reference types (such as data collections like
List
, or anyIEnumerable
, and application-specific objects), this happens when the object reference changes.Thus, you would usually need to create a
new
reference for the view-model field (such asTreeViewData = new List<MyTreeViewItem>(theUpdatedDataCollection);
) when you want the component to update.
@* Add/remove an option to see how the ComboBox reacts to the change. *@
<h4>Add option</h4>
<TelerikTextBox @bind-Value="@ValuetoAdd"></TelerikTextBox>
<TelerikButton OnClick="@AddOption">Add option</TelerikButton>
<br />
<h4>Remove the last option</h4>
<TelerikButton OnClick="@RemoveOption">Remove the last option</TelerikButton>
<br />
<h4>Load new collection</h4>
<TelerikButton OnClick="@LoadNewData">Load data</TelerikButton>
<br />
<h4>ComboBox options: @myDdlData.Count</h4>
<br />
<TelerikComboBox Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" @bind-Value="@selectedValue">
</TelerikComboBox>
@code {
string ValuetoAdd { get; set; }
int selectedValue { get; set; }
List<MyDdlModel> myDdlData = new List<MyDdlModel>(Enumerable.Range(1, 5).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x }));
void AddOption()
{
if (!string.IsNullOrWhiteSpace(ValuetoAdd))
{
myDdlData.Add(
new MyDdlModel { MyTextField = ValuetoAdd, MyValueField = myDdlData.Count + 1 }
);
myDdlData = new List<MyDdlModel>(myDdlData);
ValuetoAdd = string.Empty;
}
}
void RemoveOption()
{
if (myDdlData.Count > 0)
{
myDdlData.RemoveAt(myDdlData.Count - 1);
myDdlData = new List<MyDdlModel>(myDdlData);
}
}
void LoadNewData()
{
var newData = new List<MyDdlModel>(Enumerable.Range(6, 5).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x }));
myDdlData = new List<MyDdlModel>(newData);
Console.WriteLine("New data collection loaded.");
}
public class MyDdlModel
{
public int MyValueField { get; set; }
public string MyTextField { get; set; }
}
}