本文档介绍了在 XtraReports 中当前可用的 带区类型,说明了它们的用途,以及如何添加、编辑或删除特定报表的带区。 要学习更多关于创建报表的基本原则,请参阅 报表构建基础。
在 XtraReports 中每个报表都是 带区式 报表。 意思是一个典型的报表由许多带区组成,每个带区有不同的类型。 此外,每个带区都可以包含 报表控件。
什么是带区?
报表带区 表示在报表页上的一个特定区域,用于定义如何呈现属于其中的报表控件。 每个带区都是 Band 类的子类。 该类提供了Band.HeightF 属性(此属性指定带区在页面上占用的空间)、以及其他用于定义带区行为的特定属性。
在报表设计器 (Visual Studio 设计器和 最终用户设计器) 中,通过分部的设计界面来呈现报表带区,并以 带区分隔条 分界。
带区类型
当 添加新空白报表 到项目中时,在默认情况下,它的外观如下图所示。 可以看到,报表区域最初被划分为三个基本带区 (PageHeader、Detail 和 PageFooter 带区),为在其中放置不同的 报表控件 预留了空间。
特定的带区类型指明了如何呈现放置在其中的控件、带区的呈现顺序、以及呈现多少次(在 数据绑定报表 的情况下)。 注意,在报表设计器中,一些“带区分隔条”可能会显示如何呈现带区的提示信息。 例如,对于 PageHeader 和 PageFooter 带区,会显示“one band per page(每页一个带区)”提示。
在后面,当创建特定报表时,可以添加或移除这些带区或其他任意带区。 在 XtraReports 中有不同的带区类型可用,每个独立带区都是 Band 类的子类。 下表列出了这些带区。
报表带区类 |
说明 |
---|---|
每个页面上部的空白(也翻译为“天头”),在 PageHeaderBand 或 ReportHeaderBand 上方。 | |
在报表起始位置(也翻译为“报表标头”)。 此带区被设计用于显示某些概述信息,例如报表的封面。 | |
在每个页面的上方,在 TopMarginBand 或 ReportHeaderBand 的下方(也翻译为“页眉”)。 此带区被设计用于显示从之前页面接续的页码或表头。 | |
在每组的起始位置,或者在分跨多个页时而位于组的上方(也翻译为“分组标头”)。 此带区指定分组标准,并被用于显示每组记录起始位置的信息。 更多信息,请参阅 对数据分组。 | |
在页面上所有其他带区之间(也翻译为“细节带区”)。 此带区每行只显示绑定数据源中的一条记录,或者当没有为报表指定数据源时而简单地持有非绑定控件。 要获得更多关于数据绑定的信息,请参阅 提供数据。 | |
在 DetailBand 下方(也翻译为“从报表带区”),被设计用于在创建主/从报表时持有从报表。 从报表的主/从关系是通过 XtraReportBase.DataMember 属性指定的。 要学习更多关于从报表的内容,请参阅 主/从报表。 | |
在每组的结束位置,或者在分跨多个页时而位于组的下方(也翻译为“分组脚注”)。 此带区主要被设计用于显示每组的总结信息。 | |
在报表结束位置(也翻译为“报表脚注”)。 此带区被设计用于显示某些终极信息,例如报表总结。 | |
在每个页面的下方,在 ReportFooterBand 下方和 BottomMarginBand 上方(也翻译为“页脚”)。 此带区被设计用于从之前页面接续的页码或表尾。 | |
每个页面底部的空白(也翻译为“地脚”),在 PageFooterBand 下方。 |
下面的插图举例说明了不同带区类型的相对位置,以及在报表中呈现多少次。
在报表预览时,每页都呈现 页眉、页脚、天头 和 地脚 带区。
在报表预览时,只呈现一个 报表标头 和 报表脚注 带区。
在报表中,每组记录的 分组标头 和 分组脚注 都被呈现。
报表中 细节 带区被呈现的次数取决于从绑定数据源中返回的记录数目 —— 每条记录一个带区。
![]() |
---|
在通过 XtraReport.PrintingSystem 创建报表之后,结果文档就不包含带区实例了,而是包含了有 bricks 的页面的一个集合。 通过每个带区的报表控件来呈现这些 bricks。 (译者补注: Bricks 是 XtraPrinting 库中的一个基本术语,用于构建 XtraPrinting 库中的任意文档。每个 brick 表示页面上一个简单的有指定尺寸、内容和位置的文档元素。它可以显示文本、图像、形状、条形码、或者您能想到的任意文本和图形。有多种 Brick 类型,例如 ImageBrick 用于显示图像、ShapeBrick 用于绘制几何形状等。) |
编辑带区
特定报表的带区都被包含在 BandCollection 实例中,通过报表的 XtraReportBase.Bands 属性来访问。 可以以不同的方式来修改此集合及其元素:
-
设计时刻
选择一个报表,打开 属性 窗格并单击 XtraReportBase.Bands 属性编辑器中的省略号按钮。 这将调用 Report Editor(报表编辑器) 对话框,在其中显示了报表带区的层次树,此对话框被设计用于添加和移除带区、以及编辑带区的公共属性。
-
设计时刻
调用 Report Editor(报表编辑器) 对话框的另一种方式,是在报表的 智能标记 下拉列表中选择 Edit and Reorder Bands(编辑带区和重设置带区顺序)... 项。 注意,在设计时刻当报表获得焦点时,该智能标记被显示在报表设计器的左上角。
此外,也可以通过每个带区的智能标记来选择同一选项。
-
设计时刻
如果只需要添加特定类型的带区,那么就可以使用报表的上下文菜单来实现。 在设计时刻使用鼠标右键单击报表区域,如下图所示,您就能够把新带区插入到报表中。
-
运行时刻
下面的示例演示了如何使用 BandCollection 类的基本方法来构建一个简单的报表。 第一个方法 (AddMarginBands) 创建两个页边空白带区,并把它们添加到报表的带区集合中。 第二个方法 (RemoveMarginBands) 则从此集合中移除页边空白带区。
Show Me
在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E86。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。
注意
切记在每次通过 XtraReport.ShowPreview 方法预览已更改报表之前,都调用 XtraReport.CreateDocument 方法。
C# 复制代码
using DevExpress.XtraPrinting; using DevExpress.XtraReports.UI; // ... public class XtraReport1 : DevExpress.XtraReports.UI.XtraReport { // ... // ... // Generated code for the XtraReport1 class. // ... public void AddMarginBands() { // Check if the TopMargin band is already present in the report. if(Bands.GetBandByType(typeof(TopMarginBand)) == null) { // Create a new TopMargin band and add it to the report. TopMarginBand tmBand = new TopMarginBand(); Bands.Add(tmBand); // Create a label and set its text and width. XRLabel label = new XRLabel(); label.Text = "TopMargin Band"; label.Width = 200; // Add the label to the TopMargin band. tmBand.Controls.Add(label); } // Check if the BottomMargin band is already present in the report. if(Bands.GetBandByType(typeof(BottomMarginBand)) == null) { // Create a new BottomMargin band and add it to the report. BottomMarginBand bmBand = new BottomMarginBand(); Bands.Add(bmBand); // Create an XRPageInfo object and set its width and PageInfo option. XRPageInfo datePageInfo = new XRPageInfo(); datePageInfo.Width = 200; datePageInfo.PageInfo = PageInfo.DateTime; // Add the page information control to the BottomMargin band. bmBand.Controls.Add(datePageInfo); } } public void RemoveMarginBands() { // Obtain the TopMargin band and remove it from the report. Band band = Bands.GetBandByType(typeof(TopMarginBand)); if(band != null) Bands.Remove(band); // Obtain the BottomMargin band and remove it from the report. band = Bands.GetBandByType(typeof(BottomMarginBand)); if(band != null) Bands.Remove(band); } }
Visual Basic 复制代码
Imports DevExpress.XtraPrinting Imports DevExpress.XtraReports.UI ' ... Public Class XtraReport1 Inherits DevExpress.XtraReports.UI.XtraReport ' ... ' ... ' Generated code for the XtraReport1 class. ' ... Public Sub AddMarginBands() ' Check if the TopMargin band is already present in the report. If Bands.GetBandByType(GetType(TopMarginBand)) Is Nothing Then ' Create a new TopMargin band and add it to the report. Dim tmBand As New TopMarginBand() Bands.Add(tmBand) ' Create a label and set its text and width. Dim label As New XRLabel() label.Text = "TopMargin Band" label.Width = 200 ' Add the label to the TopMargin band. tmBand.Controls.Add(label) End If ' Check if the BottomMargin band is already present in the report. If Bands.GetBandByType(GetType(BottomMarginBand)) Is Nothing Then ' Create a new BottomMargin band and add it to the report. Dim bmBand As New BottomMarginBand() Bands.Add(bmBand) ' Create an XRPageInfo object and set its width and PageInfo option. Dim datePageInfo As New XRPageInfo() datePageInfo.Width = 200 datePageInfo.PageInfo = PageInfo.DateTime ' Add the page information control to the BottomMargin band. bmBand.Controls.Add(datePageInfo) End If End Sub Public Sub RemoveMarginBands() ' Obtain the TopMargin band and remove it from the report. Dim band As Band = Bands.GetBandByType(GetType(TopMarginBand)) If Not (band Is Nothing) Then Bands.Remove(band) End If ' Obtain the BottomMargin band and remove it from the report. band = Bands.GetBandByType(GetType(BottomMarginBand)) If Not (band Is Nothing) Then Bands.Remove(band) End If End Sub End Class
此外,下列代码被用于调用这两个方法:
C# 复制代码
XtraReport1 report = new XtraReport1(); private void btnPreview_Click(object sender, System.EventArgs e) { report.CreateDocument(); report.ShowPreview(); } private void btnAddBands_Click(object sender, System.EventArgs e) { report.AddMarginBands(); } private void btnRemoveBands_Click(object sender, System.EventArgs e) { report.RemoveMarginBands(); }
Visual Basic 复制代码
Dim Report As New XtraReport1() Private Sub BtnPreview_Click(sender As Object, e As System.EventArgs) _ Handles BtnPreview.Click Report.CreateDocument() Report.ShowPreview() End Sub Private Sub BtnAddBands_Click(sender As Object, e As System.EventArgs) _ Handles BtnAddBands.Click Report.AddMarginBands() End Sub Private Sub BtnRemoveBands_Click(sender As Object, e As System.EventArgs) _ Handles BtnRemoveBands.Click Report.RemoveMarginBands() End Sub
参阅