绑定报表
在“项目设置”中,已经在 Web 或 Windows 窗体上放入了 CrystalReportViewer 控件。在上一步中又给项目添加了 NorthwindCustomers 报表。
在本节中,将实例化 NorthwindCustomers 报表并将其绑定到 CrystalReportViewer 控件。然后,测试在没有为报表的参数字段设置当前值的情况下,报表是否正确显示。
可以采用两种方式实例化并绑定报表:
- 作为嵌入式报表。
- 作为非嵌入式报表。
从下面的过程中选择一个(不要两者都选)。
- 如果使用嵌入式报表,请按照下面这个过程将报表实例化为嵌入式报表。
- 如果使用非嵌入式报表,请按照第二个过程将报表实例化为非嵌入式报表。
将 NorthwindCustomers 报表实例化为嵌入式报表并将其绑定到 CrystalReportViewer 控件
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
使用变量名 northwindCustomersReport,为 NorthwindCustomers 报表包装类添加新的类级声明。将其访问修饰符设置为 private。
Private northwindCustomersReport As NorthwindCustomers
private NorthwindCustomers northwindCustomersReport;
在 ConfigureCrystalReports() 方法中,实例化该报表包装类。
注意 已在 “项目设置” 中创建了 ConfigureCrystalReports() 方法。
northwindCustomersReport = New NorthwindCustomers()
northwindCustomersReport = new NorthwindCustomers();
在报表实例化代码下面的一行中,将 CrystalReportViewer 控件的 ReportSource 属性绑定到已实例化的报表类(变量名:northwindCustomersReport)。
myCrystalReportViewer.ReportSource = northwindCustomersReport
crystalReportViewer.ReportSource = northwindCustomersReport;
现在即可生成并运行项目。预计报表加载将失败,因为此时尚未编写数据库登录代码。
将 NorthwindCustomers 报表实例化为嵌入式报表并将其绑定到 CrystalReportViewer 控件
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
使用变量名 northwindCustomersReport,为 NorthwindCustomers 报表包装类添加新的类级声明。将其访问修饰符设置为 private。
Private northwindCustomersReport As NorthwindCustomers
private NorthwindCustomers northwindCustomersReport;
在 ConfigureCrystalReports() 方法中,实例化该报表包装类。
注意 已在 “项目设置” 中创建了 ConfigureCrystalReports() 方法。
northwindCustomersReport = New NorthwindCustomers()
northwindCustomersReport = new NorthwindCustomers();
在报表实例化代码下面的一行中,将 CrystalReportViewer 控件的 ReportSource 属性绑定到已实例化的报表类(变量名:northwindCustomersReport)。
myCrystalReportViewer.ReportSource = northwindCustomersReport
crystalReportViewer.ReportSource = northwindCustomersReport;
现在即可生成并运行项目。预计报表加载将失败,因为此时尚未编写数据库登录代码。