Articles in this section
Category / Section

Change background of RowHeader based on business logic in WPF DataGrid

2 mins read

RowHeader is a special Column used to indicate the row status like CurrentRow, Editing status, Errors in row, etc.  You can enable or disable the RowHeader by setting the ShowRowHeader property in SfDataGrid.

RowHeader allows you to customize the background based on the business logic .By default the RowHeader has the following format as illustrated in the following screenshot.

customize the background in Grid

Figure 1: Default style of GridRowHeaderCell

You can change the background color of the RowHeader by customizing the style of the GridRowHeaderCell’s Background property based on the business logic. A converter is used to change the appropriate background color based on business logic values.

XAML

<Application.Resources>       
        <local:CustomConverter x:Key="converter"/>
        <!--Customizing the RowHeader style--> 
        <Style  TargetType= "syncfusion:GridRowHeaderCell">
            <!--By using converter the Background color is changed based on the business logic-->
            <Setter Property="Background" Value="{Binding Converter={StaticResource converter }}"/>
        </Style>
    </Application.Resources>

The Background color is changed to Red when the Status property value of the Data class is set to true else the RowHeader’s color is changed to Green Color.

C#

public class CustomConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //Type casting the value as Data class(Business logic)
            var data = value as Data;
            //The Red color is applied to RowHeader when the status value is true otherwise the Green color is applied 
            if (data.Status == true)
                return Brushes.Red;
            else
                return Brushes.Green;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

 

Note:

 Using converters in Style is applicable only for WPF and Silverlight.

 

In Silverlight, the color is applied to the corresponding business logic as illustrated in the following code example.

C#

public class ConverterClass:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //Type casting the value as Data class(Business logic)
            var data = value as Data;
            //The Red color is applied to RowHeader when the status value is true otherwise the Green color is applied 
            if (data.Status == true)
                return new SolidColorBrush(Colors.Red);
            else
                return new SolidColorBrush(Colors.Green);
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

The following screenshot displays the customized GridRowHeaderCell’s style.

customized GridRowHeaderCell’s style.

Figure 2: After customizing the GridRowHeaderCell Style

 

 

Conclusion

I hope you enjoyed learning about how to change the background of RowHeader based on the business logic in WPF DataGrid.

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example
to understand how to create and manipulate data.

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 (0)
Please sign in to leave a comment
Access denied
Access denied