从 Visual Studio 2015 年向 Crystal 报告 XI 传递参数

Passing Parameter to Crystal Reports XI from Visual Studio 2015

我 运行 遇到将参数传递给外部创建的 Crystal Reports XI 报告的问题,来自我正在构建的 WinForms 应用程序Visual Studio 2015 社区版。无论我尝试做什么,报告似乎都没有得到值,除非我在提示符下手动 select 它( 甚至不应该弹出 )显示报告时。我使用的代码与我在之前的应用程序中使用的代码相同(尽管该代码是在 VS2008 中构建的),但我已经尝试了多个 "alternate" 版本的我试图让它工作的代码。这是我目前使用的代码:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Module modReports
    Private WithEvents DocumentToPrint As New Printing.PrintDocument
    Private Sub ShowReport(ByVal LID As Integer, ByVal InHouse As Boolean)
        Dim Report As New ReportDocument
        Dim ReportParameters As ParameterFieldDefinitions = Nothing
        Dim Parameter As ParameterFieldDefinition = Nothing
        Dim ApplicationValue As ParameterDiscreteValue = Nothing
        Dim ReportValues As ParameterValues = Nothing
        Dim ReportViewer As New frmReport
        Dim Response As DialogResult = DialogResult.Cancel

        PrintingReport = True
        Report.Load(CRYSTAL_REPORT_FILE_PATH & "ExampleReport.rpt")
        Report.Refresh()
        Report.VerifyDatabase()

        ReportParameters = Report.DataDefinition.ParameterFields
        Parameter = ReportParameters.Item("PrintAll")

        ReportValues = New ParameterValues
        ApplicationValue = New ParameterDiscreteValue
        'Parameter.CurrentValues.Clear()
        'ReportValues.Clear()
        ReportValues = Parameter.CurrentValues

        If LID = 7777 Then
            ApplicationValue.Value = True
        Else
            ApplicationValue.Value = False
        End If

        ReportValues.Add(ApplicationValue)
        Parameter.ApplyCurrentValues(ReportValues)

        Response = MessageBox.Show("Do you want to send this report directly to the printer?", "SEND TO PRINTER", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

        If Response = DialogResult.No Then
            With ReportViewer
                .rptViewer.ReportSource = Nothing
                .rptViewer.ReportSource = Report
                .WindowState = FormWindowState.Maximized
                .rptViewer.RefreshReport()
                ' Set zoom level: 1 = Page Width, 2 = Whole Page, 25-100 = zoom %
                .rptViewer.Zoom(1)
                .rptViewer.Show()
                .ShowDialog()
            End With
        ElseIf Response = DialogResult.Yes Then
            Dim SelectPrinter As New PrintDialog
            Dim PrinterSelected As DialogResult = DialogResult.Cancel

            With SelectPrinter
                .Document = DocumentToPrint
                .AllowPrintToFile = False
                .AllowSelection = False
                .AllowCurrentPage = False
                .AllowSomePages = False
                .PrintToFile = False
            End With

            PrinterSelected = SelectPrinter.ShowDialog

            If PrinterSelected = DialogResult.OK Then
                Dim Copies As Integer = DocumentToPrint.PrinterSettings.Copies
                Dim PrinterName As String = DocumentToPrint.PrinterSettings.PrinterName
                Dim LastPageNumber As Integer = 1
                Dim PrintBuffer As String = String.Empty

                LastPageNumber = Report.FormatEngine.GetLastPageNumber(New ReportPageRequestContext)
                Report.PrintOptions.PrinterName = PrinterName
                Report.PrintOptions.PrinterDuplex = DocumentToPrint.PrinterSettings.Duplex
                Report.PrintToPrinter(Copies, True, 1, LastPageNumber)

                If Copies = 1 Then
                    PrintBuffer = "Printed " & Copies & " copy of "
                Else
                    PrintBuffer = "Printed " & Copies & " copies of "
                End If

                If LastPageNumber = 1 Then
                    PrintBuffer += LastPageNumber.ToString & " page."
                Else
                    PrintBuffer += LastPageNumber.ToString & " pages."
                End If

                MessageBox.Show("The report was sent to the following printer:" & vbCrLf & " • " & PrinterName & vbCrLf & PrintBuffer, "REPORT PRINTED", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If

        PrintingReport = False
    End Sub
End Module

报表本身使用 XML 文件作为数据源,该文件由该应用程序动态创建。所有这些都正常工作,而且奇怪的是,如果我将报告直接发送到打印机,它似乎可以在没有提示的情况下正确打印。只有当我尝试通过 CrystalReportViewer 对象显示报告时才会出现问题。

一些我尝试过但没有成功的事情:

如果我遍历代码,它似乎不会在任何地方出错,并且一切看起来都运行良好,直到我到达 With ReportViewer 块中的 .rptViewer.RefreshReport() 行。我已经通过应用程序验证了所有参数和值只有我 "selecting" 的值,方法是检查它们到那个点的每一步,它看起来完全符合我的预期。

但是应用程序(通过 Crystal 报告)继续提示我输入刚刚在代码中传递的值。如果我 select 该提示中的值,报告 根据我 select 的值正确生成,但无论我 "pass"编程时,提示总是默认为 True.

对于如何让此参数正确传递给 CrystalReportViewer 控件,是否有人有任何我可能忽略的建议?如果您需要任何其他信息或对我目前在此处设置的内容有任何疑问,请告诉我。提前谢谢你。

好的,根据我在 http://www.it-sideways.com/2011/10/how-to-disable-parameter-prompt-for.html 上找到的信息,似乎 CrystalReportViewer 控件的 RefreshReport 方法基本上消除了任何参数and/or 登录信息:

1. ) Do not invoke CrystalReportViewer.RefreshReport Method

This method will refresh the data for the report currently displayed in the CrystalReportViewer control. The report will prompt for parameters or logon info is necessary.

So, this method is not needed unless the same CrystalDecisions.CrystalReports.Engine.ReportDocument object is reused.

通过注释掉那行代码,我可以避免显示参数提示。进行更改后我遇到的唯一问题是,即使缩放级别设置为 1(页面宽度),并且当我 运行 项目 CrystalReportViewer 控件甚至显示它已在状态栏中正确设置(显示“缩放因子:页面宽度”),报表本身实际上并未放大到页面宽度。

在取消注释 RefreshReport 方法的情况下,如果我手动为我的参数提供值,它会显示正确缩放的报告。如果我将缩放按钮添加到控件 (.rptViewer.ShowZoomButton = True),我可以手动选择页面宽度选项,然后正确地将报告 "re-zooms" 显示到所需的级别,但它不会立即以这种方式显示如果 RefreshReport 方法没有被调用。

无论如何,我现在可以花一些时间尝试解决这个问题,但我终于可以正确地设置、传递和显示我的参数结果。我希望这可以帮助其他人 运行 解决这个问题。