|
1 | | -# How-to-populate-data-using-WebAPI-in-MAUI-DataGrid- |
2 | | -How to populate data using WebAPI in MAUI DataGrid? |
| 1 | +# How to populate data using WebAPI in MAUI DataGrid? |
| 2 | +The [.NET MAUI DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid) supports binding the item source from a Web API. |
| 3 | + |
| 4 | +##### C# |
| 5 | +Create a **JSON** data model. |
| 6 | + |
| 7 | +**OrderInfo** |
| 8 | +```C# |
| 9 | +public class OrderInfo |
| 10 | +{ |
| 11 | + public int OrderID { get; set; } |
| 12 | + public string? CustomerID { get; set; } |
| 13 | + public int EmployeeID { get; set; } |
| 14 | + public double Freight { get; set; } |
| 15 | + public string? ShipCity { get; set; } |
| 16 | + public bool Verified { get; set; } |
| 17 | + public DateTime OrderDate { get; set; } |
| 18 | + public string? ShipName { get; set; } |
| 19 | + public string? ShipCountry { get; set; } |
| 20 | + public DateTime ShippedDate { get; set; } |
| 21 | + public string? ShipAddress { get; set; } |
| 22 | +} |
| 23 | +``` |
| 24 | +Create a WebApiServices class within the Services folder. |
| 25 | + |
| 26 | +**WebApiServices** |
| 27 | + |
| 28 | +Utilize the webApiUrl to retrive the data using the ReadDataAsync() function. Read the response using the **ReadAsStringAsync()** method and deserialize the JSON object using the [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json) package. |
| 29 | + |
| 30 | +```C# |
| 31 | +internal class WebApiServices |
| 32 | +{ |
| 33 | + #region Fields |
| 34 | + |
| 35 | + public static string webApiUrl = "https://ej2services.syncfusion.com/production/web-services/api/Orders"; // Your Web Api here |
| 36 | +
|
| 37 | + HttpClient client; |
| 38 | + |
| 39 | + #endregion |
| 40 | + |
| 41 | + #region Constructor |
| 42 | + |
| 43 | + public WebApiServices() |
| 44 | + { |
| 45 | + client = new HttpClient(); |
| 46 | + } |
| 47 | + |
| 48 | + #endregion |
| 49 | + |
| 50 | + #region RefreshDataAsync |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Retrieves data from the web service. |
| 54 | + /// </summary> |
| 55 | + /// <returns>Returns the ObservableCollection.</returns> |
| 56 | + public async Task<ObservableCollection<OrderInfo>>? ReadDataAsync() |
| 57 | + { |
| 58 | + var uri = new Uri(webApiUrl); |
| 59 | + try |
| 60 | + { |
| 61 | + //Sends request to retrieve data from the web service for the specified Uri |
| 62 | + var response = await client.GetAsync(uri); |
| 63 | + |
| 64 | + if (response.IsSuccessStatusCode) |
| 65 | + { |
| 66 | + var content = await response.Content.ReadAsStringAsync(); //Returns the response as JSON string |
| 67 | + return JsonConvert.DeserializeObject<ObservableCollection<OrderInfo>>(content)!; //Converts JSON string to ObservableCollection |
| 68 | + } |
| 69 | + } |
| 70 | + catch (Exception ex) |
| 71 | + { |
| 72 | + System.Diagnostics.Debug.WriteLine(@"ERROR {0}", ex.Message); |
| 73 | + } |
| 74 | + |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + #endregion |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +**OrderInfoViewModel** |
| 83 | + |
| 84 | +Populate the data using the ReadDataAsync() in the viewmodel. |
| 85 | + |
| 86 | +```C# |
| 87 | +private async void GenerateData() |
| 88 | +{ |
| 89 | + OrderInfoCollection = await webApiServices.ReadDataAsync()!; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +The following screenshot shows how to to populate data using WebAPI in MAUI DataGrid. |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-populate-data-using-WebAPI-in-MAUI-DataGrid) |
| 98 | + |
| 99 | +Take a moment to pursue this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. |
| 100 | +Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid(SfDataGrid). |
| 101 | + |
| 102 | +### Conclusion |
| 103 | +I hope you enjoyed learning about how to populate data using WebAPI in MAUI DataGrid. |
| 104 | + |
| 105 | +You can refer to our [.NET MAUI DataGrid's feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data. |
| 106 | +For current customers, you can check out our .NET MAUI components from the [License and Downloads](https://www.syncfusion.com/account/downloads) page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components. |
| 107 | +If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://support.syncfusion.com/account/login?ReturnUrl=%2Faccount%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dc54e52f3eb3cde0c3f20474f1bc179ed%26redirect_uri%3Dhttps%253A%252F%252Fsupport.syncfusion.com%252Fagent%252Flogincallback%26response_type%3Dcode%26scope%3Dopenid%2520profile%2520agent.api%2520integration.api%2520offline_access%2520kb.api%26state%3D8db41f98953a4d9ba40407b150ad4cf2%26code_challenge%3DvwHoT64z2h21eP_A9g7JWtr3vp3iPrvSjfh5hN5C7IE%26code_challenge_method%3DS256%26response_mode%3Dquery) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid). We are always happy to assist you! |
0 commit comments