xtrareports-devexpress-dot-xtrareports-dot-ui-82861669.md
A Table control.
Namespace : DevExpress.XtraReports.UI
Assembly : DevExpress.XtraReports.v25.2.dll
NuGet Package : DevExpress.Reporting.Core
public class XRTable :
XRControl,
IWeightyContainer,
ISupportInitialize
Public Class XRTable
Inherits XRControl
Implements IWeightyContainer,
ISupportInitialize
The following members return XRTable objects:
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.
Apply the following best practices when you create an XRTable control at runtime:
The code sample below illustrates how to create a 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 columns 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;
Imports DevExpress.XtraReports.UI
' ...
' Create a report.
Dim report As New XtraReport()
' Create a detail band and add it to the report.
Dim detailBand As New DetailBand()
report.Bands.Add(detailBand)
' Create a table.
Dim table As New XRTable()
' Add rows and columns to the table.
Dim numRows As Integer = 10
Dim numCols As Integer = 20
table.BeginInit()
For i As Integer = 0 To numRows - 1
Dim row As New XRTableRow()
table.Rows.Add(row)
For j As Integer = 0 To numCols - 1
Dim cell As New XRTableCell()
table.Rows(i).Cells.Add(cell)
Next j
Next i
' 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
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 DevExpress.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 DXFont("Tahoma", 10, DXFontStyle.Bold);
tableHeader.Padding = new PaddingInfo(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 DXFont("Tahoma", 10);
tableDetail.Padding = new PaddingInfo(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);
}
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports DevExpress.Drawing
' ...
Private Shared Sub CreateDetailReport(ByVal report As XtraReport, ByVal dataMember As String)
' Create a detail report band and bind it to data.
Dim detailReportBand As New DetailReportBand()
report.Bands.Add(detailReportBand)
detailReportBand.DataSource = report.DataSource
detailReportBand.DataMember = dataMember
' Add a header to the detail report.
Dim detailReportHeader As New ReportHeaderBand()
detailReportBand.Bands.Add(detailReportHeader)
Dim tableHeader As New XRTable()
tableHeader.BeginInit()
tableHeader.Rows.Add(New XRTableRow())
tableHeader.Borders = BorderSide.All
tableHeader.BorderColor = Color.DarkGray
tableHeader.Font = New DXFont("Tahoma", 10, DXFontStyle.Bold)
tableHeader.Padding = New PaddingInfo(10)
tableHeader.TextAlignment = TextAlignment.MiddleLeft
Dim cellHeader1 As New XRTableCell()
cellHeader1.Text = "Product Name"
Dim cellHeader2 As 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.
AddHandler tableHeader.BeforePrint, AddressOf tableHeader_BeforePrint
tableHeader.EndInit()
' Create a detail band.
Dim tableDetail As New XRTable()
tableDetail.BeginInit()
tableDetail.Rows.Add(New XRTableRow())
tableDetail.Borders = BorderSide.Left Or BorderSide.Right Or BorderSide.Bottom
tableDetail.BorderColor = Color.DarkGray
tableDetail.Font = New DXFont("Tahoma", 10)
tableDetail.Padding = New PaddingInfo(10)
tableDetail.TextAlignment = TextAlignment.MiddleLeft
Dim cellDetail1 As New XRTableCell()
Dim cellDetail2 As 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 })
Dim detailBand As New DetailBand()
detailBand.Height = tableDetail.Height
detailReportBand.Bands.Add(detailBand)
detailBand.Controls.Add(tableDetail)
' Adjust the table width.
AddHandler tableDetail.BeforePrint, AddressOf tableDetail_BeforePrint
tableDetail.EndInit()
' Create and assign different odd and even styles.
Dim oddStyle As New XRControlStyle()
Dim evenStyle As 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"
End Sub
Private Shared Sub AdjustTableWidth(ByVal table As XRTable)
Dim report As XtraReport = table.RootReport
table.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right
End Sub
Private Shared Sub tableHeader_BeforePrint(ByVal sender As Object,
ByVal e As System.ComponentModel.CancelEventArgs)
AdjustTableWidth(TryCast(sender, XRTable))
End Sub
Private Shared Sub tableDetail_BeforePrint(ByVal sender As Object,
ByVal e As System.ComponentModel.CancelEventArgs)
AdjustTableWidth(TryCast(sender, XRTable))
End Sub
View Example: How to Create a Report Bound to the SQL Data Source (WinForms)
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";
}
}
}
Imports DevExpress.XtraReports.UI
Imports System.ComponentModel
' ...
Namespace WindowsFormsApplication1
<ToolboxItem(True)>
Public Class XRTableEx
Inherits XRTable
' Override the CreateRow() method to create a custom table row.
Protected Overrides Function CreateRow() As XRTableRow
Return New XRTableRowEx()
End Function
End Class
' Implement a custom table row with specified height.
Public Class XRTableRowEx
Inherits XRTableRow
Public Sub New()
MyBase.New()
SizeF = New System.Drawing.SizeF(120, Me.DefaultHeight)
End Sub
' Override the CreateCell() method to create a custom table cell.
Protected Overrides Function CreateCell() As XRTableCell
Return New XRTableCellEx()
End Function
' Specify the default row height.
Protected Overrides ReadOnly Property DefaultHeight() As Integer
Get
Return 35
End Get
End Property
End Class
' Implement a custom table cell with custom text.
Public Class XRTableCellEx
Inherits XRTableCell
Public Sub New()
Text = "MyCustomXRTableCellEx"
End Sub
End Class
End Namespace
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.
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
The following examples calculate the column width and row height to display the entire cell content:
View Example: Reporting for WinForms - How to Adjust the Column Width to Best Fit the Cell Content
View Example: Reporting for WinForms - How to Adjust the Row Height to Best Fit the Cell Content
Object MarshalByRefObject Component XRControl XRTable
See Also
Create a Report in Visual Studio