Articles in this section
Category / Section

How to load DetailsView ItemsSource asynchronously in UWP DataGrid?

2 mins read

The SfDataGrid’s DetailsViewExpanding event helps you to load the data or set ItemsSource of a DetailsView dynamically. You can use the DetailsViewSource property in GridDetailsViewExpandingEventArgs to set the itemsource for the DetailsViewDatagrid.

When the Data to be loaded in the DetailsView is downloaded from an external source or being read from a file, you may get a time delay. In such case, the Expanding event can be executed before the I/O processes get completes.

In this case, you can use Async and await to load the data with a time delay and hold the event from executing before the data gets loaded from an external source gets completed.

The await keyword ensures that no operations happen before the asynchronous method execution is completed.

The SfGrid_DetailsViewExpanding method runs synchronously until it reaches its first await expression. After await is reached, it is suspended until the awaited task is complete.

C#

//Listening to the DetailsViewExpanding Event.
//Making it Async with the Async and Await keyword to execute asynchronously.
//On accessing the DetailsViewSource property of GridDetailsViewExpandingEventArgs we can reset the ItemsSource of DetailsViewGrid
private async void SfGrid_DetailsViewExpanding(object sender, Syncfusion.UI.Xaml.Grid.GridDetailsViewExpandingEventArgs e)
{
      e.DetailsViewItemsSource.Clear();
      var list = new ObservableCollection<dynamic>();
      e.DetailsViewItemsSource.Add("Persons", list);
      var underlyingList = GetItems(list);
}
//We have Populated the data for the DetailsView Grid in this method asynchronously by holding the DetailsView ExpandingEvent
//till the data gets created or Loaded from an external source.
private async Task<ObservableCollection<dynamic>> GetItems(ObservableCollection<dynamic> dynamicList)
        {
            await Task.Delay(2000);
            dynamicList.Add(CreateDynamicObj("David", 1001, "Asst.Manager", 118));
            dynamicList.Add(CreateDynamicObj("Alex", 1002, "Senior Developer", 123));
            dynamicList.Add(CreateDynamicObj("Christopher", 1003, "Senior Developer", 121));
            dynamicList.Add(CreateDynamicObj("Mary", 1004, "Accountant", 120));
            dynamicList.Add(CreateDynamicObj("Angeline", 1005, "Asst.Manager", 105));
            dynamicList.Add(CreateDynamicObj("Andrew", 1006, "Manager", 103));
            dynamicList.Add(CreateDynamicObj("Michael", 1007, "Senior Developer", 124));
 
            return dynamicList;
        }

 

The declaration of GetItems() method adding the DetailsViewItemsSource asynchronously until the items are assigned to the underlying_List.

 

C#

private dynamic CreateDynamicObj(string name, int employeeid, string Details, int contactnumber)
        {
            ExpandoObject exp = new ExpandoObject();
            ((IDictionary<string, object>)exp).Add("Name", name);
            ((IDictionary<string, object>)exp).Add("EmployeeID", employeeid);
            ((IDictionary<string, object>)exp).Add("Details", Details);
            ((IDictionary<string, object>)exp).Add("ContactNumber", contactnumber);
            return exp;
        }

 

Sample Links:

WinRT

UWP


Conclusion

I hope you enjoyed learning about how to load DetailsView ItemsSource asynchronously in UWP DataGrid.

You can refer to our UWP DataGrid feature tour
page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our UWP DataGrid example
to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial
to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied