Back to Devexpress

FormattingRule.Condition Property

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-formattingrule-eeecd93e.md

latest6.2 KB
Original Source

FormattingRule.Condition Property

Gets or sets a string representing a Boolean expression which determines when this FormattingRule is applied.

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
[DefaultValue("")]
[SRCategory(ReportStringId.CatBehavior)]
public string Condition { get; set; }
vb
<SRCategory(ReportStringId.CatBehavior)>
<DefaultValue("")>
Public Property Condition As String

Property Value

TypeDefaultDescription
StringString.Empty

A String representing a Boolean expression.

|

Remarks

In general, a FormattingRule object is comprised of a set of formatting properties (represented by the FormattingRule.Formatting object) and a logical condition (represented by the Condition property) that determines when this formatting should be applied to a report control or a band.

The Condition property stores an expression, evaluated at report generation time. At design time, this expression can be constructed using the built-in dialog shown in the image below:

Note

Note that the type of the value returned after evaluating the Condition property must be Boolean. Otherwise, an exception occurs.

If you want to use data fields in constructing the expression, the rule’s FormattingRule.DataSource and FormattingRule.DataMember properties should be specified. By default, they are set to null ( Nothing in Visual Basic), which means that a report’s XtraReportBase.DataSource and XtraReportBase.DataMember properties are used to obtain data fields for the expression.

Note

The Condition property returns null ( Nothing in Visual Basic), until this FormattingRule object is added to a report, because it’s impossible to determine its value.

For more information on using this property, please refer to the Conditionally Changing a Control’s Appearance document.

Example

This example demonstrates how to conditionally change a control’s appearance at runtime. For this, it is necessary to create an instance of the FormattingRule class, specify its FormattingRule.Condition and FormattingRule.Formatting properties and add this object to a report’s sheet of formatting rules (XtraReport.FormattingRuleSheet) and to the collection of formatting rules of a control or a band, to which it should be applied (XRControl.FormattingRules). Note that the same task can be also solved at design time, as described in the Conditionally Changing a Control’s Appearance topic.

csharp
using System.Drawing;
using System.Drawing.Printing;
using DevExpress.XtraReports.UI;
// ...

private void XtraReport1_BeforePrint(object sender, CancelEventArgs e) {
    // Create a new rule and add it to a report.
    FormattingRule rule = new FormattingRule();
    this.FormattingRuleSheet.Add(rule);

    // Specify the rule's properties.
    rule.DataSource = this.DataSource;
    rule.DataMember = this.DataMember;
    rule.Condition = "[UnitPrice] >= 30";
    rule.Formatting.BackColor = Color.WhiteSmoke;
    rule.Formatting.ForeColor = Color.IndianRed;
    rule.Formatting.Font = new Font("Arial", 10, FontStyle.Bold);

    // Apply this rule to the detail band.
    this.Detail.FormattingRules.Add(rule);
}
vb
Imports System.Drawing
Imports System.Drawing.Printing
Imports DevExpress.XtraReports.UI
' ...

Private Sub XtraReport1_BeforePrint(ByVal sender As Object, ByVal e As CancelEventArgs) _
Handles MyBase.BeforePrint
    ' Create a new rule and add it to a report.
    Dim rule As New FormattingRule()
    Me.FormattingRuleSheet.Add(rule)

    ' Specify the rule's properties.
    rule.DataSource = Me.DataSource
    rule.DataMember = Me.DataMember
    rule.Condition = "[UnitPrice] >= 30"
    rule.Formatting.BackColor = Color.WhiteSmoke
    rule.Formatting.ForeColor = Color.IndianRed
    rule.Formatting.Font = New Font("Arial", 10, FontStyle.Bold)

    ' Apply this rule to the detail band.
    Me.Detail.FormattingRules.Add(rule)
End Sub

See Also

Conditionally Change the Control's Appearance

FormattingRule Class

FormattingRule Members

DevExpress.XtraReports.UI Namespace