Good morning,
I have a bunch of data that I would like to show in a TreeGrid. A separate thread updates some properties of the data. When this happens, I would like the TreeGrid to refresh in order to show the updated properties.
Unfortunately, I cannot make it work. I tried several approaches and I built a minimal working example based on a similar thread that I found on this forum. I uploaded the file here since the upload on the thread wouldn't work.
As you can see, when you click the "Update" button a new thread starts and it updates the data bound to the TreeGrid every 100 ms:
public void UpdateRecord()
{
Thread printer = new Thread(new ThreadStart(InvokeMethod));
printer.Start();
}
public void InvokeMethod()
{
while (true)
{
ChangeData();
Debug.WriteLine("Changed data!");
Thread.Sleep(100);
}
}
public void ChangeData()
{
Random rand = new Random();
for (var i = 0; i < TreeData.Count(); i++)
{
if (TreeData.Count() != 0)
{
int index = rand.Next(ObservableDataNew.Count());
var name = TreeData[i];
name.CustomerID = ObservableDataNew[index].CustomerID;
name.Freight = ObservableDataNew[index].Freight;
name.ShipName = ObservableDataNew[index].ShipName;
name.ShipCountry = ObservableDataNew[index].ShipCountry;
}
}
InvokeAsync(StateHasChanged);
InvokeAsync(OrdersTreeGrid.RefreshAsync);
}
Despite the fact that a spinner appears on the table, the data that it shows remain the same. How can I fix this?
Thanks for the reply. The sample you provided uses the NuGet package Syncfusion.Blazor 18.0.2.48. If you update to the latest version, i.e. Syncfusion.Blazor 19.3.0.46, the problem will show up in your sample too. I'm attaching the code.
I'm having the same problem. I'm using AddRecordAsync to update the data in the grid but nothing happens. Also 19.3.0.46. And also tested it on 18 on which it does indeed work fine.
|
<SfButton ID="update" @onclick="OnClick">Update Data</SfButton>
<SfTreeGrid @ref=TreeGrid DataSource="@TreeData" IdMapping="TaskId" ParentIdMapping="ParentId" TreeColumnIndex="1">
<TreeGridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" />
<TreeGridColumns>
<TreeGridColumn Field="TaskId" HeaderText="Task ID" Width="80" IsPrimaryKey="true" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>
. . .
</TreeGridColumns>
</SfTreeGrid>
@code{
private async Task OnClick()
{
var newRecord = new BusinessObject() { TaskId = 10, TaskName = "Child Task 10", Duration = 7, Progress = 70, Priority = "Low" };
await TreeGrid.SelectRowAsync(6);
await TreeGrid.AddRecordAsync(newRecord, 6, RowPosition.Child);
}
} |
Hello Farveen,
Sorry was indeed a different issue i was having. Your example solved mine. thanks
Thank you for the update.
Regarding this note:
Note:- There is no need to use StateHasChanged method to re-render after updating the data on updating the data.
I tried to run the same sample that I uploaded here without the StateHasChanged call (line 144 of Index.razor), but it doesn't work. Is this normal?