Conditionally Hide Controls
- 2 minutes to read
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 theDiscontinued
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 theVisible
property:[Discontinued]
Switch to preview mode to see the result:
Hide a Control at Runtime
Specify an Expression for the Visible Property
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);
}
Review the following help topic for more information: Create Expression at Runtime.
Handle the Control’s BeforePrint Event
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"));
}
How to Avoid Empty Space When a Control is Hidden
To hide a control without leaving blank space in the report, set the properties as follows:
- CanShrink
True
- Text
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])
- Visible
True
(default value)