在 Visual Studio 2005 中使用 Crystal Reports(C# .NET Windows 应用程序) [英] Using Crystal Reports in Visual Studio 2005 (C# .NET Windows App)

查看:22
本文介绍了在 Visual Studio 2005 中使用 Crystal Reports(C# .NET Windows 应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 C# .NET Windows 应用程序中创建报告.我有一个 SQL Server 2005 数据库、Visual Studio 2005 并且可以很好地创建存储过程和数据集.

I need to create reports in a C# .NET Windows app. I've got an SQL Server 2005 database, Visual Studio 2005 and am quite OK with creating stored procedures and datasets.

有人可以为我指出创建报告的正确方向吗?我只是似乎无法解决.一些示例将是一个好的开始,或者是一个简单的操作指南……任何比 MSDN 文档解释得更好的东西.

Can someone please point me in the right direction for creating reports? I just can't seem work it out. Some examples would be a good start, or a simple How-to tutorial... anything really that is a bit better explained than the MSDN docs.

我正在使用 CrystalDecisions.Windows.Forms.CrystalReportViewer 控件来显示报告,我认为这是正确的.

I'm using the CrystalDecisions.Windows.Forms.CrystalReportViewer control to display the reports, I presume this is correct.

如果我即将踏上漫长而复杂的旅程,创建和显示也可以打印的报告的最简单方法是什么?

If I'm about to embark on a long and complex journey, what's the simplest way to create and display reports that can also be printed?

推荐答案

我现在已经成功了.

简要概述

它的工作原理是拥有一个数据类",它只是一个包含变量且没有代码的常规 C# 类.然后将其实例化并填充数据,然后放置在 ArrayList 中.ArrayList 与要加载的报表名称一起绑定到报表查看器.在报表设计器中使用.Net Objects",而不是与数据库通信.

It works by having a 'data class' which is just a regular C# class containing variables and no code. This is then instantiated and filled with data and then placed inside an ArrayList. The ArrayList is bound to the report viewer, along with the name of the report to load. In the report designer '.Net Objects' are used, rather than communicating with the database.

说明

我创建了一个类来保存我的报告的数据.这个类是由我通过手动从数据库中检索数据来手动填充的.你如何做到这一点并不重要,但这里有一个例子:

I created a class to hold the data for my report. This class is manually filled by me by manually retrieving data from the database. How you do this doesn't matter, but here's an example:

DataSet ds = GeneratePickingNoteDataSet(id);
foreach (DataRow row in ds.Tables[0].Rows) {
    CPickingNoteData pickingNoteData = new CPickingNoteData();

    pickingNoteData.delivery_date = (DateTime)row["delivery_date"];
    pickingNoteData.cust_po = (int)row["CustomerPONumber"];
    pickingNoteData.address = row["CustomerAddress"].ToString();
    // ... and so on ...

    rptData.Add(pickingNoteData);
}

然后将该类放入 ArrayList 中.arraylist 中的每个元素对应于完成报告中的一个行".

The class is then put inside an ArrayList. Each element in the arraylist corresponds to one 'row' in the finished report.

列表的第一个元素也可以保存报表页眉数据,列表的最后一个元素可以保存报表页脚数据.而且因为这是一个 ArrayList,所以可以使用普通的 Array 访问来获取它们:

The first element in the list can also hold the report header data, and the last element in the list can hold the report footer data. And because this is an ArrayList, normal Array access can be used to get at them:

((CPickingNoteData)rptData[0]).header_date = DateTime.Now;
((CPickingNoteData)rptData[rptData.Count-1]).footer_serial = GenerateSerialNumber();

一旦你有一个充满数据的数组列表,像这样将它绑定到你的报表查看器,其中'rptData'的类型是'ArrayList'

Once you have an arraylist full of data, bind it to your report viewer like this, where 'rptData' is of type 'ArrayList'

ReportDocument reportDoc = new ReportDocument();
reportDoc.Load(reportPath);
reportDoc.SetDataSource(rptData);
crystalReportViewer.ReportSource = reportDoc;

现在您需要将数据类绑定到报表本身.您在设计器内部执行此操作:

Now you will need to bind your data class to the report itself. You do this inside the designer:

  1. 打开字段资源管理器"选项卡(可能位于查看"菜单下),然后右键单击数据库字段"
  2. 点击项目数据"
  3. 点击.NET 对象"
  4. 向下滚动列表以找到您的数据类(如果不存在,编译您的应用程序)
  5. 按>>"然后确定
  6. 您现在可以拖动班级成员到报告上并将它们排列为你想要的.

这篇关于在 Visual Studio 2005 中使用 Crystal Reports(C# .NET Windows 应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆