We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Async On Demand Loading

Hi,I have to load data asynchronously from the web and want to display it in a SfTreeGrid using 'On Demand Loading'. 
However I couldn't figure out a way to populate the tree on demand when I'm calling and awaiting async functions to load the data.

As an example, if modify your OnDemandLoading Example and add a await Task.Delay(1) call to the code, it won't populate the tree with tree nodes anymore.


/// <summary>
/// Gets the reportees.
/// </summary>
/// <param name="bossID">The boss ID.</param>
/// <returns></returns>
public async Task<IEnumerable<EmployeeInfo>> GetReportees(int bossID)
{
 
    List<EmployeeInfo> list = new List<EmployeeInfo>();
    int loc = FindID(bossID);
    if (loc > -1)
    {
        await Task.Delay(1);
        while (loc < this.EmployeeDetails.Count && this.EmployeeDetails[loc].ReportsTo == bossID)
            list.Add(this.EmployeeDetails[loc++]);
    }
    return list;
}


I then await this function in:
async void AssociatedObject_RequestTreeItems(object sender, TreeGridRequestTreeItemsEventArgs args)

How can I populate the SfTreeGrid on demand with data that is loaded asynchronously?

Regards and Thanks,
Frido

1 Reply

MK Muthukumar Kalyanasundaram Syncfusion Team March 30, 2017 07:07 PM UTC

Hi Frido, 

Thank you for contacting Syncfusion support. 

We have checked your query. We are able to reproduce the reported query in our end. You can overcome this problem by handling the AutoResetEvent. For your reference, we have attached the code snippet and sample in the below location, 

Code Snippet: 
 
System.Threading.AutoResetEvent autoreset = new System.Threading.AutoResetEvent(false); 
 
async void AssociatedObject_RequestTreeItems(object sender, TreeGridRequestTreeItemsEventArgs args) 
{ 
    if (args.ParentItem == null) 
    { 
        //get the root list - get all employees who have no boss  
        args.ChildItems = viewModel.EmployeeDetails.Where(x => x.ReportsTo == -1); //get all employees whose boss's id is -1 (no boss) 
    } 
    else //if ParentItem not null, then set args.ChildList to the child items for the given ParentItem. 
    {   //get the children of the parent object 
        EmployeeInfo emp = args.ParentItem as EmployeeInfo; 
        if (emp != null) 
        { 
            Task t = Task.Run(() => 
            { 
                GetChildItem(args); 
            }); 
            autoreset.WaitOne(); 
        } 
    } 
} 
 
private async void GetChildItem(TreeGridRequestTreeItemsEventArgs args) 
{ 
    EmployeeInfo emp = args.ParentItem as EmployeeInfo; 
    args.ChildItems = await viewModel.GetReportees(emp.ID); 
    autoreset.Set(); 
} 



Please let us know if you have any query. 

Regards, 
Muthukumar K 


Loader.
Live Chat Icon For mobile
Up arrow icon