`. This allows customization of the behavior of the collection.
2. **Add a flag to control notifications**
- Introduce a private boolean **flag _preventNotification** to temporarily disable collection change notifications while adding multiple items.
+ Introduce a private boolean **_preventNotification** to temporarily disable collection change notifications while adding multiple items.
3. **Override the OnCollectionChanged method**
@@ -1766,7 +1787,7 @@ To implement this functionality, follow these steps:
* Resetting **_preventNotification** to **false**.
* Raising a single **NotifyCollectionChangedAction.Reset** notification to inform the Grid that the entire collection has changed.
-The following example demonstrates how to use this approach in a Grid:
+The following example demonstrates usage of this approach in a Grid:
{% tabs %}
{% highlight razor tabtitle="Index.razor" %}
@@ -1775,41 +1796,50 @@ The following example demonstrates how to use this approach in a Grid:
@using Syncfusion.Blazor.Buttons
@using System.Collections.ObjectModel
@using System.Collections.Specialized
-@using ObservableCollection.Components.Data;
+@using ObservableCollection.Components.Data
- Add Range of Items
+ Add Range of Items
-
+
+
-
-
-
-
+
+
+
+
+
@code {
- SfGrid Grid;
- public SmartObservableCollection GridData = new SmartObservableCollection();
- public void AddRangeItems()
+ private SfGrid? grid;
+ private SmartObservableCollection gridData = new();
+
+ private void AddRangeItems()
{
- GridData.AddRange(OrdersDetailsObserveData.GetAllRecords());
+ gridData.AddRange(OrdersDetailsObserveData.GetAllRecords());
}
public class SmartObservableCollection : ObservableCollection
{
- private bool _preventNotification = false;
+ private bool preventNotification;
+
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
- if (!_preventNotification)
+ if (!preventNotification)
+ {
base.OnCollectionChanged(e);
+ }
}
+
public void AddRange(IEnumerable list)
{
- _preventNotification = true;
- foreach (T item in list)
+ preventNotification = true;
+ foreach (var item in list)
+ {
Add(item);
- _preventNotification = false;
+ }
+ preventNotification = false;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
@@ -1824,16 +1854,19 @@ namespace ObservableCollection.Components.Data
public class OrdersDetailsObserveData
{
public int? OrderID { get; set; }
- public string CustomerID { get; set; }
+ public string CustomerID { get; set; } = string.Empty;
public DateTime? OrderDate { get; set; }
public double? Freight { get; set; }
public static IEnumerable GetAllRecords()
{
+ var random = new Random();
+ var customers = new[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" };
+
return Enumerable.Range(1, 10).Select(x => new OrdersDetailsObserveData
{
OrderID = 1000 + x,
- CustomerID = (new[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
+ CustomerID = customers[random.Next(customers.Length)],
Freight = Math.Round(2.1 * x, 2),
OrderDate = DateTime.Now.AddDays(-x)
}).ToList();
@@ -1848,8 +1881,9 @@ The following screenshot represents the Grid with **Observable Collection**.

-> You can find the complete sample on [GitHub](https://github.com/SyncfusionExamples/databinding-in-blazor-datagrid/tree/master/add_range_items_observableCollection/ObservableCollection).
+> Find the complete sample on [GitHub](https://github.com/SyncfusionExamples/databinding-in-blazor-datagrid/tree/master/add_range_items_observableCollection/ObservableCollection).
## See also
-* [How to clear all Data from Grid](https://www.syncfusion.com/forums/150965/how-to-clear-all-data-from-grid)
+* [How to import data from Excel sheet and bind to Blazor Grid](https://support.syncfusion.com/kb/article/11560/how-to-import-data-from-excel-sheet-and-bind-to-blazor-grid)
+* [How to clear all Data from Grid](https://www.syncfusion.com/forums/150965/how-to-clear-all-data-from-grid)
\ No newline at end of file