在“项目设置”中,已经在 Web 或 Windows 窗体上放入了 CrystalReportViewer 控件。在上一步中,已给项目添加了 CustomersViaIDR 报表。
在本节中,将实例化 CustomersViaIDR 报表,并将其绑定到 CrystalReportViewer 控件。
可以采用两种方式实例化并绑定报表:
- 作为嵌入式报表。
- 作为非嵌入式报表。
从下面的过程中选择一个(不要两者都选)。
- 如果使用嵌入式报表,请按照下一个过程将 CustomersViaIDR 报表实例化为嵌入式报表。
- 如果使用非嵌入式报表,请按照第二个过程将 CustomersViaIDR 报表实例化为非嵌入式报表。
将 CustomersViaIDR 报表实例化为嵌入式报表并将其绑定到 CrystalReportViewer 控件
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
使用变量名 customersViaIdrReport,为 CustomersViaIDR 报表包装类添加新的类级声明。将其访问修饰符设置为 private。
Private customersViaIdrReport As CustomersViaIDRprivate CustomersViaIDR customersViaIdrReport;在 ConfigureCrystalReports() 方法中,实例化该报表包装类。
<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>已在 <a href="ms227453(v=vs.90).md">“项目设置”</a> 中创建了 ConfigureCrystalReports() 方法。</p></td>
</tr>
</tbody>
</table>
``` vb
customersViaIdrReport = New CustomersViaIDR()
```
``` csharp
customersViaIdrReport = new CustomersViaIDR();
```
在报表实例化代码下面的一行中,将 CrystalReportViewer 控件的 ReportSource 属性绑定到经过实例化的报表类(变量名:customersViaIdrReport)。
myCrystalReportViewer.ReportSource = customersViaIdrReportcrystalReportViewer.ReportSource = customersViaIdrReport;
将 CustomersViaIDR 报表实例化为非嵌入式报表并将其绑定到 CrystalReportViewer 控件
现在即可生成并运行项目。跳到下一节。
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
使用变量名 customersViaIdrReport,为 ReportDocument 报表包装类添加新的类级声明。将其访问修饰符设置为 private。
<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 类是 CrystalDecisions.CrystalReports.Engine 命名空间的成员。已在"Imports" [Visual Basic]中为此命名空间添加了 "using" [C#] 或 <a href="ms227453(v=vs.90).md">“项目设置”</a> 声明。在实例化 ReportDocument 并将报表加载到命名空间时,即通过 SDK 获取了对报表的访问,而不必嵌入报表。</p></td>
</tr>
</tbody>
</table>
``` vb
Private customersViaIdrReport As ReportDocument
```
``` csharp
private ReportDocument customersViaIdrReport;
```
- 在 ConfigureCrystalReports() 方法(在“项目设置”中创建)中,实例化 ReportDocument 类。
``` vb
customersViaIdrReport = New ReportDocument()
```
``` csharp
customersViaIdrReport = new ReportDocument();
```
声明一个字符串变量,将其命名为“reportPath”,然后将一个本地报表的运行时路径赋值给它。对于网站项目和 Windows 项目,确定此路径时会有所不同:
对于网站,要将本地报表文件的名称作为字符串参数传递到 Server.MapPath() 方法中。这样,在运行时本地报表就会映射到硬盘文件目录路径。
Dim reportPath As String = Server.MapPath("CustomersViaIDR.rpt")string reportPath = Server.MapPath("CustomersViaIDR.rpt");对于 Windows 项目,要将 Application.StartupPath 属性与一个反斜杠和本地报表文件名称连接起来。这样,报表将映射到与 Windows 可执行文件相同的目录。
注意编译时,需要将报表复制到可执行文件所在的目录。
Dim reportPath As String = Application.StartupPath & "\" & "CustomersViaIDR.rpt"string reportPath = Application.StartupPath + "\\" +"CustomersViaIDR.rpt";
调用 ReportDocument 实例的 Load() 方法,并将 reportPath 字符串变量传递给该方法。
``` vb
customersViaIdrReport.Load(reportPath)
```
``` csharp
customersViaIdrReport.Load(reportPath);
```
在报表加载部分的下一行中,将 CrystalReportViewer 的 ReportSource 属性绑定到 ReportDocument 实例。
myCrystalReportViewer.ReportSource = customersViaIdrReportcrystalReportViewer.ReportSource = customersViaIdrReport;
测试 CustomersViaIDR 报表的加载过程
无论选择通过 ReportDocument 类实例化嵌入式报表类还是实例化非嵌入式报表类,所用的变量名都是同一个:customersViaIdrReport。这使得在后面的过程中可以使用一组公共代码。
现在即可生成并运行项目。
从“生成”菜单中选择“生成解决方案”。
如果生成过程中出错,请立即纠正。
如果要在 Windows 项目中使用非嵌入式报表,请在 \bin\ [Visual Basic] 子目录或 \bin\debug\ [C#] 子目录中找到编译后的 Windows 可执行文件,然后将报表复制到该子目录。
<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>要让 Windows 可执行文件在运行时加载非嵌入式报表,该报表必须与 Windows 可执行文件存储在同一个目录中。</p></td>
</tr>
</tbody>
</table>
- 从“调试”菜单中,单击“开始”。
报表即会显示,从中可看到来自 IDataReader 静态方法的数据。
- 返回到 Visual Studio,然后单击“停止”从调试模式中退出。