How to bind the ItemSource from a JSON file in a MAUI DataGrid?
The .NET MAUI DataGrid enables you to bind the ItemsSource from a local JSON file to display data dynamically. Below is a step-by-step guide to accomplish 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; }
}
Ensure the JSON file is placed in the Resources\Raw folder in your project and set its property to MauiAsset.
OrderInfoViewModel
Read the JSON file using the FileSystem.OpenAppPackageFileAsync() method and then deserialize the JSON object using the Newtonsoft.Json package.
const string templateFileName = "OrderInfo.Json";
public async void LoadFile()
{
this.OrdersInfoCollection = new List<OrderInfo>();
using (var stream = await FileSystem.OpenAppPackageFileAsync(templateFileName))
using (var reader = new StreamReader(stream))
{
this.OrdersInfoCollection = JsonConvert.DeserializeObject<List<OrderInfo>>(await reader.ReadToEndAsync())!;
}
}
The following screenshot shows how to bind ItemsSource from a JSON file in SfDataGrid.
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to bind ItemsSource from a JSON file in SfDataGrid.
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!