如何将值传递给两个不同的参数

How to pass values to two different parameters

我正在开发 Windows 应用程序并在其中使用 Crystal 报告。

我可以将值传递给 Crystal 报告的一个参数。现在,我在 Crystal 报告中又添加了一个参数,但对我必须在下面的代码中进行哪些修改感到困惑。

下面的代码是为了给CR的一个参数传值。

Private Sub btnLoadBatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadBatch.Click
    Try
        Dim cr As New crMonthwiseBatch
        Dim oBatches As New Batches

        Dim Month As Integer = dtFrom.Value.Date.Month
        Dim StartDateForBatch As Date = New DateTime(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month, "1")
        Dim DaysinMonths As Integer = System.DateTime.DaysInMonth(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month)
        Dim EndDateForBatch = StartDateForBatch.AddDays(DaysinMonths)

        oBatches.LoadByQuery("CreatedDate >= #" + StartDateForBatch + "# and CreatedDate <= #" + EndDateForBatch + "#")
        cr.SetDataSource(oBatches)

        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue

        crParameterDiscreteValue.Value = "Batch List of Month - " + MonthName(dtBatchStartFromMonth.Value.Month) + " " + dtBatchStartFromMonth.Value.Year.ToString
        crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("MonthName")
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

        CrystalReportViewerMonthwiseBatch.ReportSource = cr
        CrystalReportViewerMonthwiseBatch.Refresh()
    Catch ex As Exception

    End Try
End Sub

您可以通过这种方式实现它(在语句上方的评论中寻找描述

方法一:

'...
'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

方法二:
如果您有多个参数,也可以通过使用单独的过程来实现,例如

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

可以为任何报表对象调用上述例程(如下例中的 cr)以向报表添加任意数量的参数

SetParams(cr, "@Param_1_Name", "Param_1_Value")
SetParams(cr, "@Param_2_Name", "Param_2_Value")
'...
SetParams(cr, "@Param_n_Name", "Param_n_Value")