我正在开发一个Windows应用程序并在其中使用Crystal Report。
我能够将值传递给Crystal Report的一个参数。现在,我在Crystal Report中添加了一个参数,但对于我在下面的代码中需要进行哪些修改感到困惑。
编写下面的代码是为了将值传递给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
答案 0 :(得分:5)
您可以通过这种方式实现(在声明上方查找评论以获取描述)
方法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
方法2:
如果您有多个参数,也可以使用单独的过程,如
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")