Skip to main content
A newer version of this page is available. .

How to: Apply Conditional Formatting to Grid Cells using the Gradient Ranges Format Condition

  • 2 minutes to read

The Range Gradient condition (FormatConditionRangeGradient) allows you to use predefined color gradients to apply conditional formatting to different ranges of values.

This example shows how to apply conditional formatting to Grid cells using the predefined Red-Blue color gradient. Use the Update Formatting button to change start/end colors and the number of ranges in the color gradient. The third color in the middle of the color scale is also specified to generate a 3-color gradient.

using DevExpress.DashboardCommon;
using System.Drawing;

namespace Grid_GradientRangeCondition {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        public Form1() {
            InitializeComponent();
            Dashboard dashboard = new Dashboard(); dashboard.LoadFromXml(@"..\..\Data\Dashboard.xml");
            dashboardViewer1.Dashboard = dashboard;
            GridDashboardItem grid = (GridDashboardItem)dashboard.Items["gridDashboardItem1"];
            GridMeasureColumn extendedPrice = (GridMeasureColumn)grid.Columns[1];

            GridItemFormatRule rangeRule = new GridItemFormatRule(extendedPrice);
            FormatConditionRangeGradient rangeCondition = 
                new FormatConditionRangeGradient(FormatConditionRangeGradientPredefinedType.RedBlue);
            rangeRule.Condition = rangeCondition;

            grid.FormatRules.AddRange(rangeRule);
        }

        private void button1_Click(object sender, System.EventArgs e) {
            GridDashboardItem grid = (GridDashboardItem)dashboardViewer1.Dashboard.Items[0];
            GridItemFormatRule rangeRule = grid.FormatRules[0];

            FormatConditionRangeGradient rangeCondition = 
                (FormatConditionRangeGradient)rangeRule.Condition;
            rangeCondition.Generate(new AppearanceSettings(Color.PaleVioletRed), 
                                    new AppearanceSettings(Color.PaleGreen), 9);
            RangeInfo middleRange = rangeCondition.RangeSet[4];
            middleRange.StyleSettings = new AppearanceSettings(Color.SkyBlue);

            rangeRule.Condition = rangeCondition;
        }
    }
}
See Also