在Winform中打开Crystal Report

我创建了一个水晶报告然后创建它后,我创建了一个winform,我导入水晶报告库(显示在代码中)并使用报告查看器查看报告,但我无法查看报告,代码,我是Crytal Reports的新手,我所做的代码是:

码:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; namespace InventorySoftware { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.reportViewer1.RefreshReport(); } private void button1_Click(object sender, EventArgs e) { //string ReportSources = ""; ReportDocument cryRpt = new ReportDocument(); cryRpt.Load("C:\\Users\\Ahsan\\Desktop\\PROJECT INVENTORY SOFTWARE\\InventorySoftware\\InventorySoftware\\CrystalReport1.rpt"); reportViewer1.ReportSource = cryRpt; reportViewer1.Refresh(); } } } 

它在reportViewer1.ReportSource = cryRpt;给出错误reportViewer1.ReportSource = cryRpt; 而错误是

 Error 1 'Microsoft.Reporting.WinForms.ReportViewer' does not contain a definition for 'ReportSource' and no extension method 'ReportSource' accepting a first argument of type 'Microsoft.Reporting.WinForms.ReportViewer' could be found (are you missing a using directive or an assembly reference?) C:\Users\Ahsan\Desktop\PROJECT INVENTORY SOFTWARE\InventorySoftware\InventorySoftware\Form1.cs 34 27 InventorySoftware 

您正在为Crystal Reports使用错误的类/控件。

在表单上放置一个CrystalReportViewer控件。 虽然对于Visual Studio的更高版本,您必须单独下载它 ,但它仍然随VS2008一起提供 。

如果在工具箱中没有看到它,请右键单击工具箱中的任意位置,然后单击“选择项目…”。

在此处输入图像描述

检查并按“确定”后,应将其添加到工具箱中。 删除现有的报表控件并在表单上删除水晶报表查看器:

在此处输入图像描述

当您将查看器放在项目上时,必要的水晶参考将添加到您的项目中。

将此using指令添加到代码隐藏的顶部:

 using CrystalDecisions.CrystalReports.Engine; 

然后将报告加载到查看器中:

 var cryRpt = new ReportDocument(); cryRpt.Load(@"C:\Users\Ahsan\Desktop\PROJECT INVENTORY SOFTWARE\InventorySoftware\InventorySoftware\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); 

编辑:

将目标框架从.NET Framework 4 Client Profile更改为.NET Framework 4

在此处输入图像描述