Crystal 报告公式字段索引在每次新构建时发生变化

Crystal Report formula field index changing on each new build

我有一份 Crystal 报告,是我在 CR2008 中制作的,我正在从我的 vb.net 应用程序中打印出来。在这份报告中,我有一个图像和一个公式字段。

图像公式(在 Format Graphic > Picture > Graphic Location 下设置为名为 @imgLocation 的公式字段。

在我的 vb.net 代码中,我有以下块 select 从数据库保存的文件路径,然后用这个值填充 @imgLocation ,以设置图像正在显示。

Try
   Dim logoPath As String = ds.Tables(0).Rows(0).Item("reportLogoPath") & ""

   If logoPath = "" Then
      MessageBox.Show("No logo path has been defined in the System Settings. Please set " & _
                       "one and try again.", "Load Report Failed", MessageBoxButtons.OK)
      Exit Sub
   Else
     cReport.DataDefinition.FormulaFields.Item(2).Text = Chr(34) & logoPath & Chr(34)
   End If

Catch ex As Exception
   errorLog(ex)
End Try

我已经看到这个工作了两次。当我有 FormulaFields.Item(2) 时,我想要的图像替换了占位符图像,但后来我调整了图像的大小,但它没有用。然后我更改了索引的值,它与 FormulaFields.Item(4) 一起工作,但当我调整图像大小时再次停止工作。

为什么索引会发生变化,解决方法是什么?

我尝试使用命名参数选项,但是 FormulaFields.Item("@imgLocation") 给我一个错误

Invalid Index

编辑

根据@Bugs 的要求,这是显示正确字符串和图像的报告

然后,删除并重新保存图像(但尺寸稍大)后,参数仍然正确传递,但显示的是占位符图像。

这让我现在想到它与图像大小有关,但是,将其重新调整回原始大小仍然没有再次显示。

编辑 2

这可能是因为我正在加载一个包含报表查看器的新表单,而不是从同一表单打开它吗?

 cReport.RecordSelectionFormula = selectionFormula
 Me.Cursor = Cursors.Default
 Dim f As New frmReportViewer(con, cReport, 0)
 f.Show()

Private Sub frmReportViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load

  Try
     setFormSizes(Me, con)
     Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2), 10)
     Me.Text = "Report Viewer"

     Dim logOnInfo As New TableLogOnInfo()
     Dim i As Integer

     For i = 0 To cReport.Database.Tables.Count - 1
          logOnInfo.ConnectionInfo.DatabaseName = "comm_db"
          logOnInfo.ConnectionInfo.Password = "Acplus2016!"
          cReport.Database.Tables.Item(i).ApplyLogOnInfo(logOnInfo)
     Next i

     cReport.VerifyDatabase()

      crViewer.ReportSource = cReport
      crViewer.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None
      crViewer.Zoom(87)

  Catch ex As Exception
      errorLog(ex)

  End Try
End Sub

Please note my answer is addressing the part regarding adding parameters. For the moment the no image available placeholder is proving difficult to resolve on my side.

要添加参数,请按照以下步骤操作:

  1. 在名为 imageLocation:

    的 Crystal 报告中添加一个参数

  2. 按下 OK 后,您的列表中应该有一个参数,如下所示:

  3. 下面的代码将一个值传递给参数:

    Dim cReport As New ReportDocument
    cReport.Load("C:\Report.rpt")
    
    If cReport.ParameterFields.Item("imageLocation") IsNot Nothing Then
        cReport.SetParameterValue("imageLocation", "\SAGE200FS\Sage\StockImages\sc002.jpg")
    End If
    
    reportViewer.ReportSource = cReport
    
  4. 这是输出(我已将参数添加到报告中):

Note that if you are passing parameters and they are shown on the report, make sure you don't have cReport.VerifyDatabase() in place. This often throws up a dialog box asking for the parameter values.

对话框截图:

不调用 cReport.VerifyDatabase() 这将停止显示对话框。

现在通过 Crystal 报告,您可以使用参数设置 图形位置 。为此,将参数添加到报告中并隐藏该字段(因为您不想看到它)。然后你可以添加 Report Field 作为位置。

  1. 添加参数进行举报和压制:

  2. 添加 Report Field 作为 图形位置 OLE Object:~=

  3. 运行 上报并输入参数。这将相应地更改图像。查看两个屏幕截图:

SC001::

INT4303:

This however does not seem to work through VB. The ReportViewer does not handle the change of images. Instead you are left with the default image. I'm unsure why this is the case and I'm guessing it's a bug with Crystal. If anyone knows of why this happens, feel free to comment.

编辑

在聊天中讨论了较小的问题后,我们似乎已经确定问题是由文件路径引起的。将它保存到数据库中,因为 \server\...\...\image.png 没有显示,但是在将它更改为 Z:\...\...\image.png 之后,它每次都运行良好。一直是文件路径的问题。