Articles in this section
Category / Section

How to change the Row background based on bool value in .NET MAUI DataGrid?

2 mins read

The .NET MAUI DataGrid provides support to customize the background of a DataGridRow based on a bool value.

XAML
Create a custom style targeting the DataGridRow in the Resource Dictionary.

<ContentPage.Resources>
    <local:ColorConverter x:Key="converter"/>
    <Style TargetType="syncfusion:DataGridRow">
        <Setter Property="Background" Value="{Binding EmployeeStatus, Converter={StaticResource Key=converter}}"/>
    </Style>
</ContentPage.Resources>

C#

In the cell tapped event, modify the bool value, which will trigger the converter.

private void SfDataGrid_CellTapped(object sender, DataGridCellTappedEventArgs e)
{
    if (e.RowData != null && viewModel.Employees.Contains(e!.RowData))
    {
        int index = viewModel.Employees.IndexOf((e.RowData as Employee)!);
        viewModel.Employees[index].EmployeeStatus = !viewModel.Employees[index].EmployeeStatus;
    }
}

In the converter, set the background based on the bool value.

public class ColorConverter : IValueConverter
{
    object IValueConverter.Convert(object? value, Type targetType, object? parameter, CultureInfo info)
    {
        if ((bool)value!)
            return Color.FromArgb("bde0fe"); 
        else
            return Colors.Transparent;
    }
    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The following GIF demonstrates the customization of DataGrid row background based on bool value.

SfDataGrid_sample.gif

Download the complete sample from GitHub.

Conclusion
I hope you enjoyed learning how to change the DataGrid row background based on a bool value in .NET MAUI DataGrid.

You can refer to our .NET MAUI DataGrid feature tour page to know 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!

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