水晶报表参数示例代码介绍

水晶报表参数示例代码介绍

Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine

Private Const RPT_NAME As String = "CustomerOrders.rpt"
Private Const PARAMETER_FIELD_NAME As String = "CustomerID"

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码

' 设置报表源
report = New ReportDocument()
report.Load(GetReportPath(RPT_NAME))

' 设置参数
crvOrders.ParameterFieldInfo = GetParameterInfo(CType(Session("list"), ListItemCollection))

crvOrders.ReportSource = report

' 显示报表
crvOrders.DataBind()

End Sub

'----------------------------------------------------------------
' Function GetParameterInfo:
' 从复选框列表获取参数字段信息
' Returns:
' 参数字段
' Parameters:
' [in] list: 复选框列表
' Throws:
'
' PreConditions:
'
'----------------------------------------------------------------
Private Function GetParameterInfo(ByVal list As ListItemCollection) As ParameterFields

Dim item As ListItem
Dim isEmpty As Boolean = True
Dim itemCount As Integer = 0

' 声明将参数传递给查看器控件所需的变量。
Dim paramFields As New ParameterFields()
Dim paramField As New ParameterField()
Dim discreteVal As New ParameterDiscreteValue()

' 参数是具有多个值的离散参数。

' 设置参数字段的名称,它必须和报表中的参数相符。
paramField.ParameterFieldName = PARAMETER_FIELD_NAME

For Each item In list
If item.Selected Then
isEmpty = False
itemCount += 1

' 设置一个离散值并将其传递给该参数
discreteVal.Value = item.Value
paramField.CurrentValues.Add(discreteVal)
' 新建一个离散值
discreteVal = New ParameterDiscreteValue()
End If
Next

If isEmpty Then
For Each item In list
' 设置一个离散值并将其传递给该参数
discreteVal.Value = item.Value
paramField.CurrentValues.Add(discreteVal)
' 新建一个离散值
discreteVal = New ParameterDiscreteValue()
Next
End If

' 将该参数添加到参数字段集合。
paramFields.Add(paramField)
GetParameterInfo = paramFields

End Function