Articles in this section
Category / Section

How to customize the Foreground color for cells in a column based on the cell content in Xamarin.Forms DataGrid?

2 mins read

SfDataGrid displays all the text values in GridCells with a default Foreground color of RGB values 51. However SfDataGrid allows you to customize the Foreground color of the GridCells for the entire view and also to some specific cells based on conditions. 

To change the Foreground color of the GridCells for the entire view, you can use the SfDataGrid.GridStyle property and set the custom style to change the foreground color. For more details please refer our user documentation in the below link.

https://help.syncfusion.com/xamarin/datagrid/styles

In case, if your requirement is to change the foreground color based on the cell value, then you can achieve this by using the CellStyle property in the GridColumn. You can write converters for the style properties based on your requirements.

Refer the below code example in which foreground color of the StudentsMarks column is customized based on conditions written in the converter.

<ContentPage.Resources>
  <ResourceDictionary>
    <local:StyleConverterForMarks x:Key="result"/>
  </ResourceDictionary>
</ContentPage.Resources>
 
<sfgrid:GridTextColumn MappingName="StudentMarks" >
  <sfgrid:GridTextColumn.CellStyle>
    <Style TargetType="sfgrid:GridCell">
      <Setter Property="Foreground"
                   Value="{BindingStudentMarks,Converter={StaticResource result}}" />
    </Style>
  </sfgrid:GridTextColumn.CellStyle>
</sfgrid:GridTextColumn>

 

Refer the below code example for writing a converter to customize the cell foreground based on conditions.

public class StyleConverterForMarks : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
                      System.Globalization.CultureInfo culture)
 {
   int _value = (int)value;
   if (_value >= 45)
   return Color.Green;
   return Color.Red;
 }
public object ConvertBack(object value, Type targetType, object parameter,
                                            System.Globalization.CultureInfo culture)
 {
   return value();
 }
}

 

Refer the following screenshot for the final outcome

DataGrid customize the cell foreground based on conditions

For more details about applying conditional styles for a column in SfDataGrid, please refer the below user documentation link.

https://help.syncfusion.com/xamarin/datagrid/conditional-styles

 

You can download the working sample for this KB from the below link.

https://www.syncfusion.com/downloads/support/directtrac/general/ze/Conditional_Styling579879886

Conclusion

I hope you enjoyed learning about how to customize the Foreground color for cells in a column based on the cell content.


You can refer to our Xamarin.Forms DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our 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 other controls.

If you have any queries or require clarifications, please let us know in the comments section 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
Please sign in to leave a comment
Access denied
Access denied