How to format column using converter in MAUI DataGrid?
The column can be formatted by using Converter in the DisplayBinding.
XAML
You can 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 column using converter in MAUI DataGrid.
Take a moment to pursue this documentation, where you can find more about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples.
Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid(SfDataGrid).
Conclusion
I hope you enjoyed learning abouthow to format column using converter in MAUI DataGrid.
You can refer to our .NET MAUI DataGrid’s feature tour 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.
For current customers, you can check out our .NET MAUI components from the License and 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.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!