在 Winform 中打开水晶报表

Open Crystal Report in Winform(在 Winform 中打开水晶报表)
本文介绍了在 Winform 中打开水晶报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a crystal report then after creating it, I've created a winform where I've import crystal report library (shown in code) and used a report viewer to view the report but i not able to view the report, the code, i am new with Crytal Reports, the code I've done is :

代码:

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; 给出错误,错误是

it's giving error at reportViewer1.ReportSource = cryRpt; and the error is

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:UsersAhsanDesktopPROJECT INVENTORY SOFTWAREInventorySoftwareInventorySoftwareForm1.cs  34  27  InventorySoftware

推荐答案

您为 Crystal Reports 使用了错误的类/控件.

You're using the wrong classes/controls for Crystal Reports.

在表单上放置一个 CrystalReportViewer 控件.尽管使用更高版本的 Visual Studio,您必须 下载单独,它仍然是随 VS2008 提供的.

Place a CrystalReportViewer control on your form. Although with later versions of Visual Studio you have to download it separately, it was still shipped with VS2008.

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

If you don't see it in your toolbox, right-click anywhere in your toolbox and click "Choose Items...".

检查并按 OK 后,它应该被添加到您的工具箱中.删除您现有的报表控件并将水晶报表查看器放在表单上:

After checking it and pressing OK, it should be added to your toolbox. Remove your existing report control and drop a crystal report viewer on the form:

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

The necessary crystal references will be added to your project when you drop the viewer on it.

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

Add this using directive to the top of your code-behind:

using CrystalDecisions.CrystalReports.Engine;

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

Then load your report into the viewer:

var cryRpt = new ReportDocument();
cryRpt.Load(@"C:UsersAhsanDesktopPROJECT INVENTORY SOFTWAREInventorySoftwareInventorySoftwareCrystalReport1.rpt");
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();

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

Change the targeted framework from .NET Framework 4 Client Profile to .NET Framework 4:

这篇关于在 Winform 中打开水晶报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)