RadzenGrid Reload on Timer

I'm attempting to hook up a RadzenGrid to a Timer so that I can refresh the page every 5 seconds.

This is the RadzenGrid properties:

RadzenGrid @ref="grid0" AllowFiltering="false" AllowPaging="false" AllowSorting="false" Data="@getVDonorTrackersResult" TItem="DonorTracker.Models.Sqlcluster.VDonorTracker"

And this is the code I have so far:

@code {
private static System.Timers.Timer aTimer;
RadzenGrid gridDT;

protected override void OnInitialized()
{
    base.OnInitialized();

	// Create a timer with a two second interval.
    aTimer = new System.Timers.Timer(5000);
    // Hook up the Elapsed event for the timer. 
    aTimer.Elapsed += OnTimedEvent;
    aTimer.AutoReset = true;
    aTimer.Enabled = true;
	
}

private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
	
    Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}", e.SignalTime);
InvokeAsync(gridDT.Reload());
}

}

The Timer is set up properly, but I'm getting an error with the grid itself:

error CS0120: An object reference is required for the non-static field, method, or property 'ViewDonorTracker.gridDT'

I appreciate any insight.

Bill D.

Hi @bdiplacido,

As the error suggests you are trying to access an instance field (gridDT) from a static method (OnTimedEvent). This isn't a valid C# construct. Deleting the static modifier from the OnTimedEvent should resolve the problem.

Hi @korchev,

I see what you're saying. I moved the Reload to the timer i.e.:

aTimer.Elapsed += (s,e)=> { InvokeAsync(gridDT.Reload); };

I can see the timer running in the console, but the grid doesn't want to refresh. Am I missing something?

Simply calling the Reload method of the DataGrid won't do much unless you also update the getVDonorTrackersResult variable. The DataGrid will just use its current value.

I figured out that if I use grid0.Reload (which is the @ref) it will successfully refresh the grid. It's all working now!

Thanks,

Bill

1 Like