本教程描述了运用DevExpress XtraReports创建drill-down report钻取报表的步骤。在这个例子中,我们将用到使用Detail Report bands细节报表区域创建的master-detail report主从报表,每个类别的细节部分可以通过点击一个适当的链接展开/折叠。
步骤如下:
1 使用Detail Report区域创建一个master-detail report主从复合结构报表。
2 创建一个显示/隐藏细节报表的链接,将标签拖放到报表的Detail细节区域上。
设置XRControl.Text的文本为Show Detail, XRControl.Name名称为lblShowHide,并设置 (DataBindings)Tag.Binding属性为CategoryID数据字段.
3 执行上述步骤后,可以得到如下报表。
4然后以如下的编程方式处理标签的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;
}
VB
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
钻取报表就完成了。运行打印预览表单,并查看结果。
更多教程: