How to Set the Font and Text Color for Rows in .NET MAUI DataGrid?
This article demonstrates how to set the FontAttributes and TextColor for all rows in the .NET MAUI DataGrid. This customization enhances the visual presentation of the data within the grid.
Xaml
<ContentPage.BindingContext>
<local:EmployeeViewModel x:Name="viewModel" />
</ContentPage.BindingContext>
<syncfusion:SfDataGrid x:Name="sfGrid"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
ColumnWidthMode="Auto"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding Employees}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTextColumn MappingName="Name"
HeaderText="Employee Name" />
<syncfusion:DataGridTextColumn MappingName="Title"
HeaderText="Designation" />
<syncfusion:DataGridTextColumn MappingName="LoginID"
HeaderText="Login ID" />
<syncfusion:DataGridTextColumn MappingName="Gender"
HeaderText="Gender" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
Xaml.cs
The code below demonstrates how to set the FontAttributes and TextColor for all rows using a custom renderer in SfDataGrid.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
sfGrid.CellRenderers.Remove("Text");
sfGrid.CellRenderers.Add("Text", new CustomTextCellRenderer());
}
}
public class CustomTextCellRenderer : DataGridTextBoxCellRenderer
{
protected override void OnSetCellStyle(DataColumnBase dataColumn)
{
base.OnSetCellStyle(dataColumn);
if (dataColumn != null)
{
DataGridCell gridCell = dataColumn.ColumnElement;
if (gridCell != null)
{
var label = gridCell.Children[0] as Label;
if (label != null)
{
label.TextColor = Colors.Orange;
label.FontAttributes = FontAttributes.Bold;
}
gridCell = null;
}
}
}
}
Output
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to set the font and text color for rows in .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 on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore 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, or the feedback portal. We are always happy to assist you!