Articles in this section
Category / Section

How to customize the text color for cells in a column based on the cell content from code behind in MAUI DataGrid (SfDataGrid)?

3 mins read

The .NET MAUI DataGrid (SfDataGrid) allows you to customize the Textcolor of the cells in a DataGridColumn based on the DataGridCell content from code behind.

XAML

<syncfusion:SfDataGrid x:Name="dataGrid" 
                      ItemsSource="{Binding OrdersInfo}"
                               AutoGenerateColumnsMode="None">
            <syncfusion:SfDataGrid.Columns>
                <syncfusion:DataGridTextColumn HeaderText="Order ID" MappingName="OrderID"/>
                <syncfusion:DataGridTextColumn HeaderText="Employee ID" MappingName="EmployeeID"/>
                <syncfusion:DataGridTextColumn HeaderText="Freight" MappingName="Freight"/>
                <syncfusion:DataGridTextColumn HeaderText="First Name" MappingName="FirstName"/>
                <syncfusion:DataGridTextColumn HeaderText="Last Name" MappingName="LastName"/>
                <syncfusion:DataGridTextColumn HeaderText="ShipCity" MappingName="ShipCity"/>
                <syncfusion:DataGridTextColumn HeaderText="Customer ID" MappingName="CustomerID"/>
            </syncfusion:SfDataGrid.Columns>
        </syncfusion:SfDataGrid>

C#

Converter

Refer the below code example for writing a converter to customize the cell TextColor based on condition.

public class StyleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double _value = (double)value;
        if (_value >= 500)
            return Colors.Green;
        return Colors.Red;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

C#

You can achieve your requirement by setting Style to DataGridColumn.CellStyle as in the below code example

public partial class MainPage : ContentPage
{
 public MainPage()
 {
  InitializeComponent();
        Style style = new Style(typeof(DataGridCell));
        style.Setters.Add(new Setter() { Property = DataGridCell.TextColorProperty, Value = new Binding("Freight", BindingMode.TwoWay, new StyleConverter()) });
        dataGrid.Columns[2].CellStyle = style;
    }
}

 

how to change the text color of the cell in code behind.

View Sample on GitHub

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 about how to you to customize the Textcolor of the cells in a DataGridColumn based on the DataGridCell content from code behind in MAUI DataGrid (SfDataGrid).

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 forumsDirect-Trac or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied