xtrareports-119439-feature-guide-to-devexpress-reports-use-expressions-expressions-tasks-and-solutions-conditionally-suppress-controls.md
This document describes how to hide a report control based on a certain logical condition when the user views/prints/exports a report. To do this, set an expression for the Visible property of the control.
The report in this topic has a column that displays a check box when the Discontinued data field value is True.
Follow the steps below to conditionally hide controls:
Drag the Discontinued data field from the Field List onto the report to create the Checkbox control bound to the Discontinued field:
Set the XRCheckBox.GlyphOptions.Alignment property to Center:
Select the control, and click the f button to invoke the Expression Editor. Enter the following expression for the Visible property:
Switch to preview mode to see the result:
At runtime, you can specify an expression as described earlier:
using DevExpress.XtraReports.UI;
public XtraReport1() {
// ...
ExpressionBinding expressionBinding = new ExpressionBinding("BeforePrint", "Visible", "[Discontinued]");
xrCheckBox1.ExpressionBindings.Add(expressionBinding);
}
Imports DevExpress.XtraReports.UI
Public Sub New()
' ...
Dim expressionBinding As New ExpressionBinding("BeforePrint", "Visibles", "[Discontinued]")
xrCheckBox1.ExpressionBindings.Add(expressionBinding)
End Sub
Review the following help topic for more information: Create Expression at Runtime.
Set the event argument’s Cancel property to True in the control’s BeforePrint event handler:
private void xrCheckBox1_BeforePrint(object sender, CancelEventArgs e) {
e.Cancel = !Convert.ToBoolean(((XRControl)sender).Report.GetCurrentColumnValue("Discontinued"));
}
Private Sub XrCheckBox1_BeforePrint(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles XrCheckBox1.BeforePrint
e.Cancel = Not Convert.ToBoolean(CType(sender, XRControl).Report.GetCurrentColumnValue("Discontinued"))
End Sub
To hide a control without leaving blank space in the report, set the properties as follows:
Empty string (‘’). To hide a control based on a condition, specify the following expression as the Text expression binding that conditionally sets its value to an empty string:
Iif(YOUR_CONDITION, '', TEXT OR [YOUR_DATA_FIELD])
VisibleTrue (default value)