绑定到已加载到 ReportDocument 类中的非嵌入式报表

对象模型

此报表绑定方案使用 ReportDocument(请参见“通过 ReportDocument 对象模型进行报表绑定”)。

报表的位置

报表位于文件目录中。

说明

此报表绑定方案允许将功能更加强大的 ReportDocument 对象模型用于非嵌入式报表(位于项目之外的报表)。

“用代码绑定到文件目录路径”中,是通过报表的文件目录路径字符串绑定到报表;但是,此方案是将目录路径传入 ReportDocument.Load() 方法,而不是将它们直接绑定到查看器。这样将可以使用 ReportDocument 对象模型。

Note注意

有关更多信息,请参见“应该使用哪种对象模型?”

另外,因为每个外部报表都被加载到公共类 ReportDocument 中,所以可以开发一个报表选择进程以共享所有报表的公共报表绑定代码。这类似于以下报表绑定方案“绑定到向上转换为 ReportDocument 的嵌入式报表类”的“实现”一节中的代码。

优点

  • 低维护:可以添加、删除或修改报表,而无需重新编译应用程序。
  • 广泛的编程交互:允许访问功能强大的 ReportDocument 对象模型。
  • 通过代码共享减少了编码工作量:类似于使用报表绑定方案“绑定到向上转换为 ReportDocument 的嵌入式报表类”,报表可以共享代码,因为所有报表都加载到公共 ReportDocument 类中。

缺点

  • 分布位置有限:报表必须和应用程序位于同一台计算机上。(通常限制从 Web 服务器的 ASPNET 用户帐户访问网络上的其他服务器。)
  • 额外的部署工作:报表必须和应用程序在同一台计算机上并位于正确的相对路径中。
  • 报表源不太安全:运行时,报表有可能在部署计算机上被重新部署/删除。

使用 ReportDocument 对象模型绑定到非嵌入式报表

Note注意

此过程仅适用于已通过“项目设置”创建的项目。“项目设置”包含此过程需要的特定命名空间引用和代码配置。如果没有该配置,将无法完成此过程。因此,在开始此过程之前,必须首先执行“项目设置”中的步骤。

  1. 将 "Imports" [Visual Basic] 或 "using" [C#] 语句添加到 CrystalDecisions.CrystalReports.Engine 命名空间的类的顶部。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>为了访问不带命名空间前缀的 ReportDocument 类,需要声明此命名空间。</p></td>
</tr>
</tbody>
</table>

``` vb
Imports CrystalDecisions.CrystalReports.Engine
```

``` csharp
using CrystalDecisions.CrystalReports.Engine;
```
  1. 在“General Business”子目录中为所使用的 Crystal Reports 版本找到 World Sales Report.rpt 文件。有关示例报表的信息,请参见“示例报表目录”

  2. 将完整的文件目录路径(包括 World Sales Report.rpt)复制到剪贴板。

  3. 在 ConfigureCrystalReports() 方法(在“项目设置”过程中创建)中,声明 reportPath 字符串变量,并将一个包含您在上一步中复制的 World Sales Report 文件目录路径的字符串赋给该变量。

    Dim reportPath As String = _
    "C:\Program Files\Microsoft Visual Studio 9.0\" _ & "Crystal Reports\Samples\chs\Reports\General Business\" _ & "World Sales Report.rpt"
    
    string reportPath =
    "C:\\Program Files\\Microsoft Visual Studio 9.0\\" + "Crystal Reports\\Samples\\chs\\Reports\\General Business\\" + "World Sales Report.rpt";
    
  4. 在字符串声明下面,声明 ReportDocument 的实例。

    Dim myReportDocument As ReportDocument = New ReportDocument()
    
    ReportDocument reportDocument = new ReportDocument();
    
  5. 将包含非嵌入式报表的文件目录路径的字符串变量加载到 ReportDocument 中。

    myReportDocument.Load(reportPath)
    
    reportDocument.Load(reportPath);
    
  6. 将 ReportDocument 实例(现在包含加载的非嵌入式报表)赋给 CrystalReportViewer 控件的 ReportSource 属性。

    myCrystalReportViewer.ReportSource = myReportDocument
    
    crystalReportViewer.ReportSource = reportDocument;
    
  7. 若要查看该报表,请生成并运行您的项目。

请参见