Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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:

  1. Drag the Discontinued data field from the Field List onto the report to create the Checkbox control bound to the Discontinued field:

    expressions-check-box

  2. Set the XRCheckBox.GlyphOptions.Alignment property to Center:

    Checkbox Alignment Center

  3. Select the control, and click the f button to invoke the Expression Editor. Enter the following expression for the Visible property:

    [Discontinued]
    

    expression-editor-iif-suppressing

  4. Switch to preview mode to see the result:

    conditional-suppressing-preview-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)