如何将值传递到两个不同的参数 [英] How to pass values to two different parameters

查看:315
本文介绍了如何将值传递到两个不同的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows应用程序,并在其中使用水晶报表。



我可以将值传递给Crystal Report的一个参数。现在,我已经为Crystal Report添加了一个参数,但是对于我在下面的代码中做了什么修改感到困惑。



下面的代码是用来将值传递给CR的一个参数。

  Private Sub btnLoadBatch_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles btnLoadBatch.Click 
尝试
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 +#和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

结束尝试
结束子


解决方案

 '... 
每个参数字段的对象集合
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
'表示参数字段
Dim crParameterFieldDefinition As ParameterFieldDefinition
每个参数字段的ParameterValue对象的集合
Dim crParameterValues As New ParameterValues()
'用于检索和设置离散值参数
Dim crParameterDiscreteValue As New ParameterDiscreteValue()

'在当前Crystal报表中获取参数集合
crParameterFieldDefinitions = cr.DataDefinition.ParameterFields

'添加FIRST参数
'获取参数在当前报告中并分配值
crParameterFieldDefinition = crParameterFieldDefinitions(PARAM_1_NAME)
crParameterDiscreteValue.Value =PARAM_1_VALUE
crParameterValues = crParameterFieldDefinition.CurrentValues

'clear分配给当前参数的旧/默认值,添加和应用新值
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
' FIRST参数

'添加SECOND和后续参数
'重置集合
crParameterDiscreteValue =新的ParameterDiscreteValue()
crParameterValues =新的ParameterValues()

'get参数,赋值,清除旧值,添加到集合并应用
crParameterFieldDefinition = crParameterFieldDefinitions(PARAM_2_NAME)
crParameterDiscreteValue.Value =PARAM_2_VALUE
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
完成添加SECOND参数
'...
'只显示报告

方法2:

如果你有多个参数,也可以通过使用一个单独的过程,如

  Sub SetParams(crRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument,strParamName As String,strParamValue As String)
对于每个pfField As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition在crRpt.DataDefinition.ParameterFields
如果pfField.Name .ToString()。ToLower()= strParamName.ToLower()然后
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
'这里添加您的异常处理机制
MessageBox.Show(ex.Message)
结束尝试
结束如果
下一个
结束Sub

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

  SetParams 
SetParams(cr,@ Param_2_Name,Param_2_Value)
'...
SetParams(cr,@Param_n_Name,Param_n_Value)
b $ b


I am developing a Windows Application and use a Crystal Report in it.

I am able to pass value to one parameter of Crystal Report. Now, I have added one more parameter to Crystal Report, but confused about what modification I have to make in below code.

Below code is written to pass value to one parameter of 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

解决方案

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")

这篇关于如何将值传递到两个不同的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆