Skip to main content
A newer version of this page is available. .

XRTable Class

A Table control.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v21.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 Table at Runtime

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 and add it to the detail band.
XRTable table = new XRTable();
detailBand.Controls.Add(table);

// 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();

The following code sample creates a new SqlDataSource, creates a report with the XRTable control at runtime, and binds the table to data:

Table Report in Code

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraReports.UI;
// ...
private static XtraReport CreateReport()
{
    // Creates a new data source.
    SqlDataSource sqlDataSource = CreateSQLDataSource();

    // Creates a new report and assigns the data source.
    XtraReport report = new XtraReport();
    report.DataSource = sqlDataSource;
    report.DataMember = "selectQuery";

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

    // Creates a table and adds it to the detail band.
    XRTable table = new XRTable();
    detailBand.Controls.Add(table);

    // Creates a row and adds the product name and product price cells to the row.
    table.BeginInit();

    XRTableRow row = new XRTableRow();
    table.Rows.Add(row);

    XRTableCell productName = new XRTableCell();
    XRTableCell productPrice = new XRTableCell();

    row.Cells.Add(productName);
    row.Cells.Add(productPrice);

    // Binds the table cells to the data fields.
    productName.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[ProductName]"));
    productPrice.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[UnitPrice]"));

    // Adjust the table width.
    table.BeforePrint += Table_BeforePrint;

    table.EndInit();
    return report;
}

private static void Table_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    XtraReport report = (sender as XRTable).RootReport;
    (sender as XRTable).WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;
}
private static SqlDataSource CreateSQLDataSource()
{
    // Creates a data source. 
    SQLiteConnectionParameters connectionParameters = new SQLiteConnectionParameters("Data/nwind.db", "");
    SqlDataSource dataSource = new SqlDataSource(connectionParameters);

    // Creates a SELECT query.
    SelectQuery query = SelectQueryFluentBuilder
        .AddTable("Products")
        .SelectColumn("ProductName")
        .SelectColumn("UnitPrice")
        .Build("selectQuery");

    // Adds the query to the collection and returns the data source. 
    dataSource.Queries.Add(query);
    dataSource.Fill();
    return dataSource;
}

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.

See Also