How to format column using converter in MAUI DataGrid?
This article explains how to format a column using a converter in .NET MAUI DataGrid.
The column can be formatted by using a Converter in the DisplayBinding.
XAML
Utilize the Converter property within the binding to format the column.
<ContentPage.Resources>
<converter:CurrencyFormatConverter x:Key="currencyFormatConverter"/>
</ContentPage.Resources>
<dataGrid:SfDataGrid x:Name="dataGrid"
ColumnWidthMode="Auto"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
ItemsSource="{Binding Employees}">
<dataGrid:SfDataGrid.Columns>
<dataGrid:DataGridNumericColumn MappingName="EmployeeID" HeaderText="Employee ID"/>
<dataGrid:DataGridTextColumn MappingName="Name" />
<dataGrid:DataGridNumericColumn MappingName="Salary"
HeaderText="Salary"
DisplayBinding="{Binding Path=Salary,Converter={StaticResource currencyFormatConverter}}"/>
</dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
C#
public MainPage()
{
InitializeComponent();
//Equivalent to the code in the XAMl section
DataGridTextColumn salaryColumn = new DataGridTextColumn() { MappingName = "Salary", HeaderText = "Salary" };
salaryColumn.DisplayBinding = new Binding() { Path = "Salary", Converter = new CurrencyFormatConverter() };
dataGrid.Columns.Add(salaryColumn);
}
CurrencyFormatConverter
public class CurrencyFormatConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return string.Format("{0:C}", value);
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The following screenshot shows how to format a column using the converter in .NET MAUI DataGrid.
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to format columns using a converter 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!