在Winform中打开Crystal Report

时间:2014-04-17 20:08:42

标签: c# winforms visual-studio-2008 crystal-reports

我创建了一个水晶报告然后在创建之后,我创建了一个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;给出了错误,错误是

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

1 个答案:

答案 0 :(得分:5)

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

在表单上放置CrystalReportViewer控件。虽然在Visual Studio的更高版本中,您必须download it separately,但仍然是shipped with VS2008

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

enter image description here

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

enter image description here

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

将此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

enter image description here