Articles in this section
Category / Section

How to populate data using WebAPI in MAUI DataGrid?

3 mins read

The .NET MAUI DataGrid supports binding the item source from a Web API, enabling dynamic data retrieval and display. Here’s a detailed guide to help you achieve this.

C#

Create a JSON data model.

OrderInfo

public class OrderInfo
{
    public int OrderID { get; set; }
    public string? CustomerID { get; set; }
    public int EmployeeID { get; set; }
    public double Freight { get; set; }
    public string? ShipCity { get; set; }
    public bool Verified { get; set; }
    public DateTime OrderDate { get; set; }
    public string? ShipName { get; set; }
    public string? ShipCountry { get; set; }
    public DateTime ShippedDate { get; set; }
    public string? ShipAddress { get; set; }
}

Create a WebApiServices class within the Services folder.

WebApiServices

Utilize the webApiUrl to retrieve the data using the ReadDataAsync() function. Read the response using the ReadAsStringAsync() method and deserialize the JSON object using the Newtonsoft.Json package.

internal class WebApiServices
{
    #region Fields

    public static string webApiUrl = "https://ej2services.syncfusion.com/production/web-services/api/Orders"; // Your Web Api here

    HttpClient client;

    #endregion

    #region Constructor

    public WebApiServices()
    {
        client = new HttpClient();
    }

    #endregion

    #region RefreshDataAsync

    /// <summary>
    /// Retrieves data from the web service.
    /// </summary>
    /// <returns>Returns the ObservableCollection.</returns>
    public async Task<ObservableCollection<OrderInfo>>? ReadDataAsync()
    {
        var uri = new Uri(webApiUrl);
        try
        {
            //Sends request to retrieve data from the web service for the specified Uri
            var response = await client.GetAsync(uri);

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync(); //Returns the response as JSON string
                return JsonConvert.DeserializeObject<ObservableCollection<OrderInfo>>(content)!; //Converts JSON string to ObservableCollection
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(@"ERROR {0}", ex.Message);
        }

        return null;
    }
    
    #endregion
}

OrderInfoViewModel

Populate the data using the ReadDataAsync() in the view model.

private async void GenerateData()
{
    OrderInfoCollection = await webApiServices.ReadDataAsync()!;
}

The following screenshot shows how to populate data using WebAPI in the MAUI DataGrid.

SfDataGrid_WebApi.png

Download the complete sample from GitHub

Conclusion

I hope you enjoyed learning how to populate data using WebAPI in the .NET MAUI DataGrid.

You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-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  to leave a comment
Access denied
Access denied