Grid And INotifyPropertyChange

In the article here https://blazor.syncfusion.com/documentation/datagrid/data-binding/#observable-collection
I have ammended this method
      public void UpdateRecords()
    {
        var a = GridData.First();
        a.CustomerID = "Update";
    }
to    
    public async Task UpdateRecords()
        {
            for (int i = 0; i < 10; i++)
            {
                var a = GridData.First();
                a.CustomerID = $"Update No.{i}";
                await Task.Delay(1000);
            }

        }
I notice that the grid only updates the on the first iteration of the loop. Why is this? 

3 Replies 1 reply marked as answer

VN Vignesh Natarajan Syncfusion Team June 17, 2020 04:37 AM UTC

Hi Paul,  
 
Thanks for contacting Syncfusion support.  
 
Query: “I notice that the grid only updates the on the first iteration of the loop. Why is this?” 
 
From your query we suspect that you are facing issue (value gets updated on first iteration only) while updating a value with Task.Delay. But we are not able to reproduce the reported issue at our end. We have modified your code example as below to update the corresponding record value. Values gets updated without any error. Refer the below code example.   
 
public async Task UpdateRecords()    {        for (int i = 0; i < 10; i++)        {            var a = GridData[i];            a.CustomerID = $"Update No.{i}";                    }    }
 
 
Note: we have also ensured the reported issue with Task.Delay(1000). Values gets updated after some time.    
 
For your convenience we have prepared a sample which can be downloaded from below  
 
Kindly get back to us with more details if we misunderstood your query.    
 
Regards, 
Vignesh Natarajan 


Marked as answer

PS paul stanley June 17, 2020 06:21 AM UTC

Thank you for your response and the sample project. I was just trying to test the grid to update the customerId periodically in the background.
I presume that this is a threading issue, I noticed the UpdateRecords is called on a worker thread. How can I demonstrate updating the field periodically?


VN Vignesh Natarajan Syncfusion Team June 18, 2020 04:25 AM UTC

Hi Paul,  
 
Thanks for the update.   
 
Query: “I noticed the UpdateRecords is called on a worker thread. How can I demonstrate updating the field periodically? 
 
We suggest you to achieve your requirement using Timer and calling StateHasChanged inside the TimerHandler. Refer the below code example.  
 
<button @onclick="StartTimer">Start Update</button> 
<button @onclick="StopTimer">Stop Update</button> 
  
<SfGrid DataSource="@GridData" AllowReordering="true"> 
    <GridColumns> 
. . . .. . . .  
    </GridColumns> 
</SfGrid> 
  
  
@code{ 
    private static System.Timers.Timer aTimer; 
    public void StopTimer() 
    { 
        aTimer.Stop(); 
    } 
  
    public async Task StartTimer() 
    { 
        // Create a timer with a two second interval. 
        aTimer = new System.Timers.Timer(2000); 
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += async (sender, e) => await HandleTimer(); 
        aTimer.Start(); 
    } 
    private async Task HandleTimer() 
    { 
        var a = GridData.First(); 
        a.CustomerID = (new string[] { "ALFKI""ANANTR""ANTON""BLONP""BOLID" })[new Random().Next(5)]; 
        await InvokeAsync(StateHasChanged); 
    } 
} 
 
 
Kindly download the sample from below  
 
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan 
 


Loader.
Up arrow icon