本指南描述了创建 drill-down 报表 的步骤。 在本例中,我们使用一个根据从报表带区创建的 主/从报表,它的每个类别的细节带区都可以通过 单击适当的链接 而被展开/折叠。 要查看总说明,请参阅 Drill-Down 报表。
要创建 drill-down 报表,则执行下列操作。
-
为了创建一个显示/隐藏从报表的链接,我们把一个标签控件拖放到报表的细节带区。
把它的 XRControl.Text 属性修改为 Show Detail、XRControl.Name 属性修改为 lblShowHide,并把 (DataBindings)Tag.Binding 属性设置为 CategoryID 数据字段。
-
在完成上述步骤之后,报表的外观应该像下图所示的那样。
-
现在,以下列方式接管标签控件的 BeforePrint、PreviewClick 和 PreviewMouseMove 事件以及 DetailReport 的 BeforePrint 事件:
C# 复制代码 using System.Collections; using System.Windows.Forms; using System.Drawing.Printing; using DevExpress.XtraReports.UI; // ... // Declare two string constants, to store the label's Text value. const string sShowDetail = "Show Detail"; const string sHideDetail = "Hide Detail"; // Create an array containing IDs of the categories being expanded. ArrayList expandedValues = new ArrayList(); // This function returns a value indicating whether a certain // category's details should be expanded. bool ShouldShowDetail(int catID) { return expandedValues.Contains(catID); } private void lbShowHide_BeforePrint(object sender, PrintEventArgs e) { XRLabel label = (XRLabel)sender; // Choose the label's text. if(ShouldShowDetail((int)label.Tag)) { label.Text = sHideDetail; } else { label.Text = sShowDetail; } } private void DetailReport_BeforePrint(object sender, PrintEventArgs e) { // Cancel the Detail Report band's printing if necessary. e.Cancel = !ShouldShowDetail((int)GetCurrentColumnValue("CategoryID")); } private void lbShowHide_PreviewClick(object sender, PreviewMouseEventArgs e) { // Obtain the category's ID stored in the label's Tag property. int index = (int)e.Brick.Value; // Determine whether the current category's details are shown. bool showDetail = ShouldShowDetail(index); // Toggle the visibility of the category's details. if(showDetail) { expandedValues.Remove(index); } else { expandedValues.Add(index); } // Re-create a document to apply the changes. CreateDocument(); } // The following code changes the cursor to "hand" when it hovers the label, // so that it behaves as a common link. private void lbShowHide_PreviewMouseMove(object sender, PreviewMouseEventArgs e) { Cursor.Current = Cursors.Hand; }
Visual Basic 复制代码 Imports System.Collections Imports System.Windows.Forms Imports System.Drawing.Printing Imports DevExpress.XtraReports.UI ' ... ' Declare two string constants, to store the label's Text value. Private Const sShowDetail As String = "Show Detail" Private Const sHideDetail As String = "Hide Detail" ' Create an array containing IDs of the categories being expanded. Private expandedValues As New ArrayList() ' This function returns a value indicating whether a certain ' category's details should be expanded. Private Function ShouldShowDetail(ByVal catID As Integer) As Boolean Return expandedValues.Contains(catID) End Function Private Sub lbShowHide_BeforePrint(ByVal sender As Object, ByVal e _ As PrintEventArgs) Handles lbShowHide.BeforePrint Dim label As XRLabel = CType(sender, XRLabel) ' Choose the label's text. If ShouldShowDetail(CInt(Fix(label.Tag))) Then label.Text = sHideDetail Else label.Text = sShowDetail End If End Sub Private Sub DetailReport_BeforePrint(ByVal sender As Object, ByVal e _ As PrintEventArgs) Handles DetailReport.BeforePrint ' Cancel the Detail Report band's printing if necessary. e.Cancel = Not ShouldShowDetail(CInt(Fix(GetCurrentColumnValue("CategoryID")))) End Sub Private Sub lbShowHide_PreviewClick(ByVal sender As Object, ByVal e _ As PreviewMouseEventArgs) Handles lbShowHide.PreviewClick ' Obtain the category's ID stored in the label's Tag property. Dim index As Integer = CInt(Fix(e.Brick.Value)) ' Determine whether the current category's details are shown. Dim showDetail As Boolean = ShouldShowDetail(index) ' Toggle the visibility of the category's details. If showDetail Then expandedValues.Remove(index) Else expandedValues.Add(index) End If ' Re-create a document to apply the changes. CreateDocument() End Sub ' The following code changes the cursor to "hand" when it hovers the label, ' so that it behaves as a common link. Private Sub lbShowHide_PreviewMouseMove(ByVal sender As Object, ByVal e _ As PreviewMouseEventArgs) Handles lbShowHide.PreviewMouseMove Cursor.Current = Cursors.Hand End Sub
现在 drill-down 报表已经就绪。 运行打印预览窗体,并查看结果。
Show Me |
---|
在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E769。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。 |