You can achieve it this way (look for comments above statement(s) for description)
Approach 1:
'...
'collection of objects for every parameter field
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
'represent a parameter field
Dim crParameterFieldDefinition As ParameterFieldDefinition
'collection of ParameterValue objects for every parameter field
Dim crParameterValues As New ParameterValues()
'for retrieving and setting discrete value parameters
Dim crParameterDiscreteValue As New ParameterDiscreteValue()
'get parameters collection in current crystal report
crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
'adding FIRST parameter
'get the parameter in the current report and assign a value
crParameterFieldDefinition = crParameterFieldDefinitions("PARAM_1_NAME")
crParameterDiscreteValue.Value = "PARAM_1_VALUE"
crParameterValues = crParameterFieldDefinition.CurrentValues
'clear old/default values assigned to current parameter, add and apply new value
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
'finished adding FIRST parameter
'adding SECOND and subsequent parameters
'reset the collections
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterValues = New ParameterValues()
'get parameter, assign values, clear old values, add to collection and apply
crParameterFieldDefinition = crParameterFieldDefinitions("PARAM_2_NAME")
crParameterDiscreteValue.Value = "PARAM_2_VALUE"
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
'finished adding SECOND parameter
'...
'Just display the report
Approach 2:
If you have multiple parameters it can also be achieved by using a separate procedure such as
Private Sub SetParams(crRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument, strParamName As String, strParamValue As String)
For Each pfField As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition In crRpt.DataDefinition.ParameterFields
If pfField.Name.ToString().ToLower() = strParamName.ToLower() Then
Try
Dim crParameterValues As New CrystalDecisions.Shared.ParameterValues()
Dim crParameterDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue()
crParameterDiscreteValue.Value = strParamValue
crParameterValues = pfField.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
pfField.ApplyCurrentValues(crParameterValues)
Catch ex As Exception
'add your exception handling mechanism here
MessageBox.Show(ex.Message)
End Try
End If
Next
End Sub
The above routine can be called for any report object (as cr in example below) to add any number of parameters to the reports
SetParams(cr, "@Param_1_Name", "Param_1_Value")
SetParams(cr, "@Param_2_Name", "Param_2_Value")
'...
SetParams(cr, "@Param_n_Name", "Param_n_Value")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…