Creating Conditional Formatting Rules
- 4 minutes to read
This topic describes how to create conditional formatting rules.
- Creating Conditional Formatting Rules at Design Time
- Creating Conditional Formatting Rules in Code
- Creating Conditional Formatting Rules at Runtime
Creating Conditional Formatting Rules at Design Time
The GridControl provides the following ways of creating conditional formatting rules at design time:
- Adding Rules Using Conditional Formatting Menu
- Adding Rules Using Conditional Formatting Rules Manager
Adding Rules Using Conditional Formatting Menu
The Conditional Formatting Menu allows you to quickly create conditional formatting rules using predefined settings.
In the design view, right-click a column to which you want to add a conditional formatting rule and select Conditional Formatting to invoke the conditional formatting menu.
Note
Specify the type of records contained in the data source using the DataControlBase.DesignTimeDataObjectType property to make the conditional formatting menu correctly work.
Adding Rules Using Conditional Formatting Rules Manager
The Conditional Formatting Rules Manager allows you to create conditional formatting rules with custom settings. You can view and edit all conditional formatting rules currently applied to the grid’s view.
Use the following ways to invoke the conditional formatting rules manager:
In the design view, right-click a column and select Conditional Formatting | Manage Rules.
In the design view, right-click the GridControl’s smart tag menu and select Manage Conditional Formatting Rules.
In the design view, right-click the GridColumn’s smart tag menu and select Manage Conditional Formatting Rules.
Note
Specify the type of records contained in the data source using the DataControlBase.DesignTimeDataObjectType property to make the conditional formatting rules manager correctly work.
Creating Conditional Formatting Rules in Code
The TableView.FormatConditions (or TreeListView.FormatConditions) collection stores conditional formatting rules applied to the grid’s view.
The following code sample illustrates how to define a conditional formatting rule in markup. See the How to: Apply Conditional Formatting example to learn more.
<dxg:TableView.FormatConditions>
<dxg:DataBarFormatCondition FieldName="Visits" PredefinedFormatName="GreenGradientDataBar" />
</dxg:TableView.FormatConditions>
The code sample below illustrates how to define the same conditional formatting rule in code-behind. See the How to: Apply Conditional Formatting in Code-Behind example to learn more.
var visitsDataBar = new DataBarFormatCondition() {
FieldName = "Visits",
PredefinedFormatName = "GreenGradientDataBar"
};
view.FormatConditions.Add(visitsDataBar);
Use the following methods to remove conditional formatting rules:
- TableView.ClearFormatConditionsFromAllColumns (or TreeListView.ClearFormatConditionsFromAllColumns) - Removes all conditional formatting rules applied to the grid’s view.
- TableView.ClearFormatConditionsFromColumn (or TreeListView.ClearFormatConditionsFromColumn) - Removes conditional formatting rules for the specified column from the conditional format collection.
Creating Conditional Formatting Rules at Runtime
The GridControl provides the following ways of creating conditional formatting rules at runtime:
- Adding Rules Using Conditional Formatting Menu
- Adding Rules Using Conditional Formatting Rules Manager
Adding Rules Using Conditional Formatting Menu
The Conditional Formatting Menu allows you to quickly create conditional formatting rules using predefined settings.
Right-click a column to which you want to add a conditional formatting rule, and select Conditional Formatting in the displayed context menu to invoke the conditional formatting menu.
You can enable the conditional formatting menu as follows:
- Set the view’s TableView.AllowConditionalFormattingMenu (or TreeListView.AllowConditionalFormattingMenu) property to true.
- Set the column’s ColumnBase.AllowConditionalFormattingMenu property to true to enable conditional formatting menu for the required column. This approach has higher priority than setting the view’s property.
Adding Rules Using Conditional Formatting Rules Manager
The Conditional Formatting Rules Manager allows you to create conditional formatting rules with custom settings. You can view and edit all conditional formatting rules currently applied to the grid’s view.
Click the Manage Rule item in the Conditional Formatting Menu to invoke the conditional formatting rules manager.
Set the TableView.AllowConditionalFormattingManager (or TreeListView.AllowConditionalFormattingManager) property to true to enable the manager at runtime.