Skip to main content

Hide Table Cells

  • 3 minutes to read

This help topic describes how you can hide a table cell conditionally, based on the report parameter value.

Prerequisites

Start with a Table report bound to the Products table of the Sample Northwind Database.

Right-click the Parameters section in the Field List and select Add Parameter.

field-list-add-parameter

In the invoked Add New Parameter dialog, specify the name (showCatID) and description (Show Category ID), and set the type to Boolean:

table-hiding-cell-parameter-settings

Methods to Hide a Cell

You can use the following methods to hide a table cell.

Specify an Expression for the Visible Property

Specify an expression for the cell’s Visible property to define a logical condition to display or hide that cell.

In this example, the showCatID report parameter determines whether the cell bound to the CategoryID field is displayed. The expression bound to the Visible property is as follows:

?showCatID

table-hide-cell-using-visible-expression

You should also specify the same expression for the Visibility property of the Category ID cell in the header table.

Handle the XtraReport.BeforePrint Event

Handle the report’s BeforePrint event to set the table cell’s Visible property to False when a certain condition is met.

private void XtraReport1_BeforePrint(object sender, System.ComponentModel.EventArgs e) {
    if (Convert.ToBoolean(showCatID.Value)) {
        xrTableCell2.Visible = true;
        xrTableCell6.Visible = true;
    }
    else {
        xrTableCell2.Visible = false;
        xrTableCell6.Visible = false;
    }
}

Handle the XRTableCell.BeforePrint Event

Set the event argument’s Cancel property to True in the cell’s BeforePrint event handler:

private void xrTableCell2_BeforePrint(object sender, System.ComponentModel.EventArgs e) {
    if (!Convert.ToBoolean(showCatID.Value))
        e.Cancel = true;
}

private void xrTableCell6_BeforePrint(object sender, System.ComponentModel.EventArgs e) {
    if (!Convert.ToBoolean(showCatID.Value))
        e.Cancel = true;
}

How to Fill the Gap Caused by a Hidden Cell

Use the ProcessHiddenCellMode property to define how to distribute the remaining space between visible table cells.

table-process-hidden-cell-mode-property

The image below shows the original table:

table-hidden-cell-mode-initial-layout

The following processing modes are available:

StretchPreviousCell

A cell located to the left of the hidden cell is stretched to occupy the available space. If the hidden cell is the first in the row, the next cell is stretched.

table-hidden-cell-mode-stretch-previous-cell

StretchNextCell

A cell located to the right of the hidden cell is stretched to occupy the available space. If the hidden cell is the last in the row, the previous cell is stretched.

table-hidden-cell-mode-stretch-next-cell

ResizeCellsEqually

All visible cells are resized to equally divide the space that a hidden cell previously occupied before it was hidden.

table-hidden-cell-mode-resize-cells-equally

ResizeCellsProportionally

All visible cells are resized to proportionally divide the space that a hidden cell previously occupied based on their weights relative to the entire table width.

table-hidden-cell-mode-resize-cells-proportionally

DecreaseTableWidth

The table width is decreased, and visible cells are shifted to a hidden cell’s location without changing their size.

table-hidden-cell-mode-decrease-table-width

LeaveEmptySpace

The default mode. An empty space remains at a hidden cell’s location and other cells are not affected.

table-hidden-cell-mode-leave-empty-space

Preview the Result

The resulting report appears as follows:

Hide Table Cells - Resulting Report