This is a migrated thread and some comments may be shown as answers.

Change the color of GridViewCell Based on value when using DataTable

1 Answer 40 Views
GridView
This is a migrated thread and some comments may be shown as answers.
rahat
Top achievements
Rank 1
rahat asked on 24 Sep 2019, 06:42 AM
Hi 
I am trying to find a solution for changing the color of RadGridView based on the value. In "CellStyleSelector" example I have to define what the style is but in my case the background color can be one of many colors .Cell Background becomes darker shades of Red if value increase , darker  shades of green when value goes below 0 and vice versa. I am unable to figure out a way to do it. 

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar Dinev
Telerik team
answered on 25 Sep 2019, 01:49 PM

Hello Rahat,

The best way to achieve the desired behavior is to use a converter, since there are multiple conditions.

public class ValueToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int count = (int)value;
            if (count < 0 && count >=-2)
                return new SolidColorBrush(Colors.Orange);
            else if (count < -2)
                return new SolidColorBrush(Colors.Red);
            else if (count > 0 && count <=2)
                return new SolidColorBrush(Colors.GreenYellow);
            else if (count > 2)
                return new SolidColorBrush(Colors.Green);
            return new SolidColorBrush(Colors.White);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
Attached, you can find a sample project demonstrating this approach. Please, review it and let me know if it delivered the desired result.

Regards,
Dimitar Dinev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
rahat
Top achievements
Rank 1
Answers by
Dimitar Dinev
Telerik team
Share this question
or