How can I help you?
Getting Started with .NET MAUI DataGrid
15 Jun 202618 minutes to read
This section provides a quick overview for working with the SfDataGrid for .NET MAUI. Follow the steps below to add a basic DataGrid to your project.
To quickly get started with the .NET MAUI DataGrid, watch this video:
Prerequisites
Before proceeding, ensure the following are set up:
- Install .NET 9 SDK or later.
- Set up a .NET MAUI environment with Visual Studio 2022 v17.12 or later.
Step 1: Create a new .NET MAUI Project
- Go to File > New > Project and choose the .NET MAUI App template.
- Name the project and choose a location. Then, click Next.
- Select the .NET framework version and click Create.
Step 2: Install the Syncfusion® MAUI DataGrid NuGet Package
- In Solution Explorer, right-click the project and choose Manage NuGet Packages.
- Search for Syncfusion.Maui.DataGrid and install the latest version.
- Ensure the necessary dependencies are installed correctly, and the project is restored.
Step 3: Register the handler
Syncfusion.Maui.Core NuGet is a dependent package for all Syncfusion® controls of .NET MAUI. In the MauiProgram.cs file, register the handler for Syncfusion® core.
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Syncfusion.Maui.Core.Hosting;
namespace GettingStarted
{
public class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
builder.ConfigureSyncfusionCore();
return builder.Build();
}
}
}Step 4: Add a Basic DataGrid
- Import the control namespace
Syncfusion.Maui.DataGridin XAML or C# code. - Initialize the SfDataGrid control.
<ContentPage
. . .
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid">
<syncfusion:SfDataGrid />
</ContentPage>using Syncfusion.Maui.DataGrid;
. . .
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
SfDataGrid dataGrid = new SfDataGrid();
this.Content = dataGrid;
}
}Prerequisites
Before proceeding, ensure the following are set up:
- Install .NET 9 SDK or later.
- Set up a .NET MAUI environment with Visual Studio Code.
- Ensure that the .NET MAUI workloads are installed and configured as described here.
Step 1: Create a new .NET MAUI Project
- Open the command palette by pressing
Ctrl+Shift+Pand type .NET:New Project and enter. - Choose the .NET MAUI App template.
- Select the project location, type the project name and press Enter.
- Then choose Create project.
Step 2: Install the Syncfusion® MAUI DataGrid NuGet Package
- Press Ctrl + ` (backtick) to open the integrated terminal in Visual Studio Code.
- Ensure you’re in the project root directory where your .csproj file is located.
- Run the command
dotnet add package Syncfusion.Maui.DataGridto install the Syncfusion® .NET MAUI DataGrid package. - To ensure all dependencies are installed, run
dotnet restore.
Step 3: Register the handler
Syncfusion.Maui.Core NuGet is a dependent package for all Syncfusion® controls of .NET MAUI. In the MauiProgram.cs file, register the handler for Syncfusion® core.
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Syncfusion.Maui.Core.Hosting;
namespace GettingStarted
{
public class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
builder.ConfigureSyncfusionCore();
return builder.Build();
}
}
}Step 4: Add a Basic DataGrid
- Import the control namespace
Syncfusion.Maui.DataGridin XAML or C# code. - Initialize the SfDataGrid control.
<ContentPage
. . .
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid">
<syncfusion:SfDataGrid />
</ContentPage>using Syncfusion.Maui.DataGrid;
. . .
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
SfDataGrid dataGrid = new SfDataGrid();
this.Content = dataGrid;
}
}Prerequisites
Before proceeding, ensure the following are set up:
- Install .NET 9 SDK or later.
- Set up a .NET MAUI environment with JetBrains Rider 2024.3 or later.
- Make sure the MAUI workloads are installed and configured as described here.
Step 1: Create a new .NET MAUI Project
- Go to File > New Solution, Select .NET (C#) and choose the .NET MAUI App template.
- Enter the Project Name, Solution Name, and Location.
- Select the .NET framework version and click Create.
Step 2: Install the Syncfusion® MAUI DataGrid NuGet Package
- In Solution Explorer, right-click the project and choose Manage NuGet Packages.
- Search for Syncfusion.Maui.DataGrid and install the latest version.
- Ensure the necessary dependencies are installed correctly, and the project is restored. If not, Open the Terminal in Rider and manually run:
dotnet restore
Step 3: Register the handler
Syncfusion.Maui.Core NuGet is a dependent package for all Syncfusion® controls of .NET MAUI. In the MauiProgram.cs file, register the handler for Syncfusion® core.
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Syncfusion.Maui.Core.Hosting;
namespace GettingStarted
{
public class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
builder.ConfigureSyncfusionCore();
return builder.Build();
}
}
}Step 4: Add a Basic DataGrid
- Import the control namespace
Syncfusion.Maui.DataGridin XAML or C# code. - Initialize the SfDataGrid control.
<ContentPage
. . .
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid">
<syncfusion:SfDataGrid />
</ContentPage>using Syncfusion.Maui.DataGrid;
. . .
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
SfDataGrid dataGrid = new SfDataGrid();
this.Content = dataGrid;
}
}Step 5: Define the View Model
Data Model
Create a simple data model as shown in the following code example, and save it as OrderInfo.cs file:
public class OrderInfo
{
private string orderID;
private string customerID;
private string customer;
private string shipCity;
private string shipCountry;
public string OrderID
{
get { return orderID; }
set { this.orderID = value; }
}
public string CustomerID
{
get { return customerID; }
set { this.customerID = value; }
}
public string ShipCountry
{
get { return shipCountry; }
set { this.shipCountry = value; }
}
public string Customer
{
get { return this.customer; }
set { this.customer = value; }
}
public string ShipCity
{
get { return shipCity; }
set { this.shipCity = value; }
}
public OrderInfo(string orderId, string customerId, string country, string customer, string shipCity)
{
this.OrderID = orderId;
this.CustomerID = customerId;
this.Customer = customer;
this.ShipCountry = country;
this.ShipCity = shipCity;
}
}NOTE
If you want your data model to respond to property changes, implement the
INotifyPropertyChangedinterface in your model class.
View Model
Create a model repository class with OrderInfo collection property initialized with the required number of data objects in a new class file as shown in the following code example, and save it as OrderInfoRepository.cs file:
public class OrderInfoRepository
{
private ObservableCollection<OrderInfo> orderInfo;
public ObservableCollection<OrderInfo> OrderInfoCollection
{
get { return orderInfo; }
set { this.orderInfo = value; }
}
public OrderInfoRepository()
{
orderInfo = new ObservableCollection<OrderInfo>();
this.GenerateOrders();
}
public void GenerateOrders()
{
orderInfo.Add(new OrderInfo("1001", "Maria Anders", "Germany", "ALFKI", "Berlin"));
orderInfo.Add(new OrderInfo("1002", "Ana Trujillo", "Mexico", "ANATR", "Mexico D.F."));
orderInfo.Add(new OrderInfo("1003", "Ant Fuller", "Mexico", "ANTON", "Mexico D.F."));
orderInfo.Add(new OrderInfo("1004", "Thomas Hardy", "UK", "AROUT", "London"));
orderInfo.Add(new OrderInfo("1005", "Tim Adams", "Sweden", "BERGS", "London"));
orderInfo.Add(new OrderInfo("1006", "Hanna Moos", "Germany", "BLAUS", "Mannheim"));
orderInfo.Add(new OrderInfo("1007", "Andrew Fuller", "France", "BLONP", "Strasbourg"));
orderInfo.Add(new OrderInfo("1008", "Martin King", "Spain", "BOLID", "Madrid"));
orderInfo.Add(new OrderInfo("1009", "Lenny Lin", "France", "BONAP", "Marsiella"));
}
}Step 6: Binding the ViewModel
Create a ViewModel instance and set it as the DataGrid’s BindingContext. This enables property binding from ViewModel class.
To populate the SfDataGrid, bind the item collection from its BindingContext to SfDataGrid.ItemsSource property.
The following code example binds the collection created in the previous step to the SfDataGrid.ItemsSource property:
<ContentPage.BindingContext>
<local:OrderInfoRepository x:Name="viewModel" />
</ContentPage.BindingContext>
<ContentPage.Content>
<syncfusion:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding OrderInfoCollection}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridNumericColumn HeaderText="Order ID" Format="0"
MappingName="OrderID" Width="150"/>
<syncfusion:DataGridTextColumn HeaderText="Customer"
MappingName="Customer"
Width="150" />
<syncfusion:DataGridTextColumn HeaderText="Ship City"
MappingName="ShipCity"
Width="150" />
<syncfusion:DataGridTextColumn HeaderText="Ship Country"
MappingName="ShipCountry"
Width="150" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
</ContentPage.Content>OrderInfoRepository viewModel = new OrderInfoRepository();
SfDataGrid dataGrid = new SfDataGrid();
dataGrid.ItemsSource = viewModel.OrderInfoCollection;
this.Content = dataGrid;
You can download the complete project of this demo from GitHub.
NOTE
You can refer to our .NET MAUI DataGrid feature tour page for its groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Example that shows you how to render the DataGrid in .NET MAUI.