Skip to main content

XRTable Class

A Table control.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v23.2.dll

NuGet Package: DevExpress.Reporting.Core

Declaration

public class XRTable :
    XRControl,
    IWeightyContainer,
    ISupportInitialize

Remarks

The XRTable control displays data in tabular form. Use the XRTable.Rows property to access table rows, and the XRTableRow.Cells property to access a row’s cells collection.

Create a Simple Table

Apply the following best practices when you create an XRTable control at runtime:

  • Always call the XRTable.BeginInit and XRTable.EndInit methods if you modify XRTable row and cell collections at runtime.
  • Set the required width of the XRTable control. When you add rows and cells to the table, their widths will be adjusted to fit the set table width.
  • Explicitly specify the table height only if you do not expect cell content to stretch the cell height (for instance, when the cell’s XRTableCell.CanGrow property is enabled).

The code sample below illustrates how to create an XRTable at runtime.

using DevExpress.XtraReports.UI;
// ...
// Create a report.
XtraReport report = new XtraReport();

// Create a detail band and add it to the report.
DetailBand detailBand = new DetailBand();
report.Bands.Add(detailBand);

// Create a table.
XRTable table = new XRTable();

// Add rows and colums to the table.
int numRows = 10;
int numCols = 20;

table.BeginInit();

for (int i = 0; i < numRows; i++) {
    XRTableRow row = new XRTableRow();
    table.Rows.Add(row);

    for (int j = 0; j < numCols; j++) {
        XRTableCell cell = new XRTableCell();
        table.Rows[i].Cells.Add(cell);
    }
}

// Set table size.
table.HeightF = 50;
table.WidthF = 500;

table.EndInit();

// Add the table to the detail band and adjust the band height.
detailBand.Controls.Add(table);
detailBand.HeightF = table.HeightF;

Create a Data-Bound Table with a Header

The code creates new Report Header and Detail bands at the detail level, and adds a table control to each band.

Table cells in the Detail band are bound to the data fields of the report data source. The table uses different styles for odd and even rows.

The width of both tables is set to the effective page width.

using DevExpress.DataAccess.Sql;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System.Drawing;
// ...
private static void CreateDetailReport(XtraReport report, string dataMember)
{
    // Create a detail report band and bind it to data.
    DetailReportBand detailReportBand = new DetailReportBand();
    report.Bands.Add(detailReportBand);
    detailReportBand.DataSource = report.DataSource;
    detailReportBand.DataMember = dataMember;

    // Add a header to the detail report.
    ReportHeaderBand detailReportHeader = new ReportHeaderBand();
    detailReportBand.Bands.Add(detailReportHeader);

    XRTable tableHeader = new XRTable();
    tableHeader.BeginInit();
    tableHeader.Rows.Add(new XRTableRow());
    tableHeader.Borders = BorderSide.All;
    tableHeader.BorderColor = Color.DarkGray;
    tableHeader.Font = new Font("Tahoma", 10, FontStyle.Bold);
    tableHeader.Padding = 10;
    tableHeader.TextAlignment = TextAlignment.MiddleLeft;

    XRTableCell cellHeader1 = new XRTableCell();
    cellHeader1.Text = "Product Name";
    XRTableCell cellHeader2 = new XRTableCell();
    cellHeader2.Text = "Unit Price";
    cellHeader2.TextAlignment = TextAlignment.MiddleRight;

    tableHeader.Rows[0].Cells.AddRange(new XRTableCell[] { cellHeader1, cellHeader2 });
    detailReportHeader.Height = tableHeader.Height;
    detailReportHeader.Controls.Add(tableHeader);

    // Adjust the table width.
    tableHeader.BeforePrint += tableHeader_BeforePrint;
    tableHeader.EndInit();

    // Create a detail band.
    XRTable tableDetail = new XRTable();
    tableDetail.BeginInit();
    tableDetail.Rows.Add(new XRTableRow());
    tableDetail.Borders = BorderSide.Left | BorderSide.Right | BorderSide.Bottom;
    tableDetail.BorderColor = Color.DarkGray;
    tableDetail.Font = new Font("Tahoma", 10);
    tableDetail.Padding = 10;
    tableDetail.TextAlignment = TextAlignment.MiddleLeft;

    XRTableCell cellDetail1 = new XRTableCell();
    XRTableCell cellDetail2 = new XRTableCell();
    cellDetail2.TextAlignment = TextAlignment.MiddleRight;

    cellDetail1.ExpressionBindings.Add(
        new ExpressionBinding("BeforePrint", "Text", "[ProductName]"));
    cellDetail2.ExpressionBindings.Add(
        new ExpressionBinding("BeforePrint", "Text",
        "FormatString('{0:$0.00}', [UnitPrice])"));


    tableDetail.Rows[0].Cells.AddRange(new XRTableCell[] { cellDetail1, cellDetail2 });

    DetailBand detailBand = new DetailBand();
    detailBand.Height = tableDetail.Height;
    detailReportBand.Bands.Add(detailBand);
    detailBand.Controls.Add(tableDetail);

    // Adjust the table width.
    tableDetail.BeforePrint += tableDetail_BeforePrint;
    tableDetail.EndInit();

    // Create and assign different odd and even styles.
    XRControlStyle oddStyle = new XRControlStyle();
    XRControlStyle evenStyle = new XRControlStyle();

    oddStyle.BackColor = Color.WhiteSmoke;
    oddStyle.StyleUsing.UseBackColor = true;
    oddStyle.Name = "OddStyle";

    evenStyle.BackColor = Color.White;
    evenStyle.StyleUsing.UseBackColor = true;
    evenStyle.Name = "EvenStyle";

    report.StyleSheet.AddRange(new XRControlStyle[] { oddStyle, evenStyle });

    tableDetail.OddStyleName = "OddStyle";
    tableDetail.EvenStyleName = "EvenStyle";
}

private static void AdjustTableWidth(XRTable table)
{
    XtraReport report = table.RootReport;
    table.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;
}

static void tableHeader_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
    AdjustTableWidth(sender as XRTable);
}

static void tableDetail_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
    AdjustTableWidth(sender as XRTable);
}

View Example: How to Create a Report Bound to the SQL Data Source (WinForms)

Create a Custom Table

Implement descendants of the XRTable and XRTableRow classes to create a custom table (for instance, a table with a different default row height or with pre-populated cells). Override the protected virtual XRTable.CreateRow and XRTableRow.CreateCell methods. These methods are invoked in all designer operations, except for multiple item drops from the Field List.

The code sample below illustrates custom table implementation.

using DevExpress.XtraReports.UI;
using System.ComponentModel;
// ...

namespace WindowsFormsApplication1 {
    [ToolboxItem(true)]
    public class XRTableEx : XRTable {
        // Override the CreateRow() method to create a custom table row.
        protected override XRTableRow CreateRow() {
            return new XRTableRowEx();
        }
    }

    // Implement a custom table row with specified height.
    public class XRTableRowEx : XRTableRow {
        public XRTableRowEx()
            : base() {
            SizeF = new System.Drawing.SizeF(120, this.DefaultHeight);
        }

        // Override the CreateCell() method to create a custom table cell.
        protected override XRTableCell CreateCell() {
            return new XRTableCellEx();
        }

        // Specify the default row height.
        protected override int DefaultHeight {
            get {
                return 35;
            }
        }
    }

    // Implement a custom table cell with custom text.
    public class XRTableCellEx : XRTableCell {
        public XRTableCellEx() {
            Text = "MyCustomXRTableCellEx";
        }
    }
}

Convert Labels to Table

You can convert multiple labels to a table and then align the table instead of aligning each label separately. See the Convert Labels to Table topic for more information.

Merge Cells with Identical Values

Use the the control’s ProcessDuplicatesMode and ProcessDuplicatesTarget properties to merge cells with the same values. For more information specific to controls, refer to the following topics:

View Example: Reporting for WinForms - How to Vertically Merge Cells With Duplicate Values

Implements

See Also