Click here to Skip to main content
Click here to Skip to main content

Crystal Report Basics and Integration with DataSet

By , 9 Apr 2007
Rate this:
Please Sign up or sign in to vote.

Introduction

Crystal report is a report design tool which allows creating reports capable of retrieving and formatting a result set from a database or other datasource. In addition to simply reading data from a data source, Crystal report has its own formula languages for creating calculations and includes a number of features that can be used to turn raw data into presentation-quality reports, with graphs, charts, running totals and so on. This article explains how crystal reports are using in C# Windows based application. Visual Studio 2003 .NET contains a bundled version of Crystal report version 9 and we can use the reports inside the developing environment.

Overview

Report Objects

The Crystal Report Designer uses a drag-and-drop approach similar to Visual Studio .NET — you drag a report object onto the Designer (such as a database field or text object) and use the Property Window or shortcut menu to format the object.

Some report objects that you can add to your report and format according to your needs include:

  • Database fields
  • Formula fields
  • Parameter fields
  • Group Name fields
  • Running Total fields
  • Summary fields
  • Charts
  • Subreports

Report Sections

The Crystal Report Designer is divided into report sections, such as section headers, footers, and details. You drag objects onto a report section.

The data that appears in the finished report is contingent upon your design choices. In particular, report data varies depending on the sections into which you choose to insert particular report objects. For example, if you insert a chart object in the Report Header section, the chart will appear only once at the beginning of the report and will summarize the data contained in the report. Alternatively, if a chart object is added to the Group Header section, a separate chart will appear at the beginning of each group of data and will summarize the data relating only to that group.

Report Header

Objects placed in the Report Header section print once, at the beginning of the report.

The Report Header section generally contains the report title and other information you want to appear only at the beginning of the report. Charts and cross-tabs placed in this section contain data for the entire report. Formulas placed in this section are evaluated once, at the beginning of the report.

Page Header

Objects placed in the Page Header section print at the beginning of each new page.

The Page Header section generally contains information that you want to appear at the top of each page. This can include text fields such as chapter names, document name, or other similar information. This section can also be used to contain field titles, which will then display as labels above the columns of field data in the report. Charts or cross-tabs cannot be placed in this section. Formulas placed in this section are evaluated once per page, at the beginning of each new page.

Details

Objects placed in the Details section print with each new record.

The Details section contains data for the body of the report. The bulk of the report data usually appears in this section. When the report is run, the Details section is re-printed for each record. For example, if you add a database object to the Details section, and this database object contains 100 records, then the report will print 100 individual detail sections at runtime. Charts or cross-tabs cannot be placed in this section. Formulas placed in this section are evaluated once for each record.

Report Footer

Objects placed in the Report Footer section print once at the end of the report.

This section is used to contain information you want to appear only once at the end of the report, such as grand totals. Charts and cross-tabs placed in this section contain data for the entire report. Formulas placed in this section are evaluated once, at the end of the report.

Page Footer

Objects placed in the Page Footer section print at the bottom of each page.

This section usually contains the page number and any other information you want to appear at the bottom of each page. Charts and cross-tabs cannot be placed in this section. Formulas placed in this section are evaluated once per page, at the end of each new page.

Additional Report Sections

If a group, summary, or subtotal is added to the report, the program creates two additional sections: the Group Header and the Group Footer.

When a group, summary, or subtotal is added, the Group Header section appears directly above the Details section, and the Group Footer section appears directly below the Details section. Like the original report sections, each of these newly added sections could contain one or more subsections. By default, they each contain a single section.

Group header

Objects placed in the Group Header section print at the beginning of each new group.

This section typically holds the group name field, and it can also be used to display charts or cross-tabs that include data specific to the group. The Group Header section is printed once at the beginning of each group. Charts and cross-tabs placed in this section contain data just for the group. Formulas placed in this section are evaluated once for each group, at the beginning of the group.

Group Footer

Objects placed in the Group Footer section print at the end of each group.

This section generally holds the summary value, if any, and can be used to display charts or cross-tabs. The Group Footer section is printed once at the end of each group. Charts and cross-tabs placed in this section contain data just for the group. Formulas placed in this section are evaluated once for each group, at the end of the group.

Showing Report In the Application

Adding Windows form viewers to Application

The CrystalReportViewer control allows a Crystal Report to be viewed in an application. The CrystalReportViewer allows users to navigate through different pages (go to the first page, last page, next page, previous page and go to a page number) and provides features to print report, export report to a particular format (i.e. .pdf, .doc, .xls and .rtf formats). It also provides the report to view in different zoom levels.

Steps to add a CrystalReportViewer in a form are as follows:

  1. Open the Toolbox and drag a CrystalReportViewer onto your form
  2. Resize and move the Windows Forms Viewer as desired by dragging and dropping
  3. When you run your application, the report is displayed in the viewer.

Screenshot - Image_2.jpg

Image shows a crystal report viewer inside a form in a Windows based application

Adding Reports in the application

For adding crystal reports to our application, right click the project and select add-> add new item menu. The following dialog box will come. We can select the crystal report from the stated dialog.

Screenshot - Image_3.jpg

The Crystal report is a file with .rpt extension. While adding the report, the developing environment automatically binds it with a class file with the same name of the report. The class file contains the resource name i.e. the .rpt file, all the sections inside the .rpt file and some other information. This .cs file is used for the interface between the report and the application.

Setting data to Crystal Report

Adding .xsd file to project

The XML Designer in VS 2003 developing environment supports the XML Schema Definition (XSD) language defined by the World Wide Web Consortium (WC3). Schema view provides a visual representation of the elements, attributes, types, and so on that make up XML Schemas and ADO.NET datasets. For adding an XSD file, right click on the project and select Add-> Add new item and select the Data Set as shown below.

Screenshot - Image_4.jpg

While adding an XSD file, the developing environment automatically binds it with a class file (.cs) and .xsx file. The class file contains runtime classes of XSD schema file. The generated classes can be used in conjunction with System.XML.Serialization.XMLSerializer to read and write XML code that follows the schema. The .xsx file holds the layout information for components on the designer surface.

Creating data tables

Open the xsd file and from the toolbox, click element object and drag to the work area. This operation will add a table to the dataset. We can design the table according to our requirements and it provides almost all the data types for a table design. We can add any number of tables into one dataset.

Screenshot - Image_5.jpg

Map the tables with Crystal Report

Double click the crystal report to view the crystal report designer. Select the field explorer window, right click the Database fields and select Add/Remove Database sub menu as shown below. If the field explorer window is not present then select View->Other windows -> Document outline or press Ctrl+Alt+T to see the same.

Screenshot - Image_6.jpg

From the next Window expand the project data node. (The project Data contains all the project datasets (datasets contained in the .xsd files)). From there, we can select the previously added dataset (CrystalDemo.DatasetDemo) and select the table names that we want to bind with crystal report and click the right arrow and press OK.

Screenshot - Image_7.jpg

If more than one table is selected from the Database Expert dialog, then after clicking OK another dialog will come asking the link between tables.

Screenshot - Image_8.jpg

We can browse the dataset in the report designer.

Screenshot - Image_9.jpg

The next step is to click the required fields from the field explorer and drag to the Crystal Report and place it in the detailed section (Normally the detailed section contains the data).

Screenshot - Image_10.jpg

Binding Reports to Crystal Report Viewer at run time

Now the crystal report is ready to display. We have to supply data for displaying and need to set the report to crystal report viewer. For that, first have to create a DataTable with name table1 and dynamically add columns with the same name of the previous data table (created in the previous section – in xsd file). Give data to the data table. The next step is to create an instance of the Crystal report and set the allocated data table as the data source. Then assign the report to the crystal report viewer's report source. Run the application. Below sample code gives an idea about displaying a report.

   // Create a data table with the same name of crystal report's table.
   demoTable = new DataTable("table1");
   // Create table columns with the same fields name of crystal report's table.
   demoTable.Columns.Add("Name");
   demoTable.Columns.Add("Age");
   demoTable.Columns.Add("Address");
   demoTable.Columns.Add("Phone");
   // Adding 1000 rows to the data table
   for (int index = 1; index <= 1000; index++)
   {
    Random rnd = new Random();
    // Create a data row of the created table.
    DataRow row = demoTable.NewRow();
    // Assign values to each cell. We can assign any type of data to
    // the cell (object type)
    row["Name"] = "Prathap";
    row["Age"] = rnd.Next(10,100);
    row["Address"] = "Trivandrum";
    row["Phone"] = "954712449198";
    demoTable.Rows.Add(row);
   }
   // Create instance of the crystal report.
   CrystalReportDemo report = new CrystalReportDemo();
   // Set data source to the crystal report. If the report contains multiple
   // datatables, then we can index each tables by using the table
   // name. Following code explains how table name is used for indexing.
   report.Database.Tables["table1"].SetDataSource((DataTable)demoTable);
   // Assign the report to the Crystal report viewer.
   crystalReportViewer.ReportSource = report;

Author Profile

Screenshot - Prathap.jpg

He is working as Software Engineer at Network Systems & Technologies (p) Ltd and is currently associated with NeST Avionics.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Prathapachandran.v
Technical Lead Network Systems & Technologies (NeST)
India India
I have been working as a software engineer for 8 years. Working experience is mainly in C# windows platform.
Skill Sets
============
.NET Technologies : C#,WPF,WCF
Other Languages : QT, OpenGL, Visual C++, Visual
RDBMS : SQL-Server, PostgreSQL

Comments and Discussions

 
QuestionGetting Crystal Login Prompt Pinmembertallion31-Jan-14 11:28 
Questionincrease width Pinmemberkillomp11-May-13 7:22 
QuestionTechnically well done PinmemberAdam1212Bell24-Mar-13 13:18 
Questionwww.reportingsoftware.info Pinmemberhannahdreadz21-Sep-12 0:42 
QuestionCrystalReportViewer not in my Toolbox PinmemberKen Than6-Jan-12 6:11 
Questioncode of create a vb.net application PinmemberPoojaPanchal6-Mar-11 19:48 
GeneralDisplay Table2 details After Table 1 Completes PinmemberLouisLewis29-Jun-10 13:32 
Questioncrystal report Pinmembervivek kumar11-Apr-09 4:09 
GeneralQuery on crystal report tool PinmemberBheema8426-Mar-09 13:34 
GeneralBinding Object as datasource for crystal reports. PinmemberXandip18-Jan-09 22:07 
GeneralGreat Article Pinmemberviv281226-Apr-08 14:32 
Questionhow to change width of page in crystal report ? Pinmemberaz_farzaneh8-Jan-08 21:46 
AnswerRe: how to change width of page in crystal report ? PinmemberAsif_ku17-Sep-09 7:09 
Generalreport more than one table Pinmembertornado19797-Dec-07 12:51 
Questionto display vertical result of a query in crystal report Pinmemberaz_farzaneh28-Nov-07 0:03 
AnswerRe: to display vertical result of a query in crystal report PinmemberPrathapachandran.v28-Nov-07 0:16 
GeneralRe: to display vertical result of a query in crystal report Pinmemberaz_farzaneh28-Nov-07 19:56 
GeneralRe: to display vertical result of a query in crystal report PinmemberPrathapachandran.v28-Nov-07 20:11 
QuestionWill you please tell me how to ceate Guntt Chart in it? Pinmemberuranus122523-Sep-07 22:50 
QuestionAre you really using XSD? PinmemberSoftEngi7-Aug-07 21:01 
AnswerRe: Are you really using XSD? PinmemberPrathapachandran.v7-Aug-07 21:42 
GeneralRe: Are you really using XSD? PinmemberSoftEngi7-Aug-07 22:54 
GeneralATGrid Report Control Pinmemberetcell30-Jul-07 18:45 
ATGrid报表控件简介
下载地址:
http://www.etcell.com/
http://www.etcell.com/download.aspx?id=51
 
ATGrid报表控件是在ETCell报表控件基础上推出的第二代报表控件,是一款完全对象化的报表组件。
ATGrid在功能上相对ETCell做出了重大改善:
支持多个ETSet数据集
支持7个子对象,最小对象到单元格
支持33种单元格数据类型
支持任意复杂格式报表数据展现
支持23种图表展示
扩展xml支持,操作数据更加灵活方便
改进打印功能,更好的支持套打、连续打印、分页打印
提供了ASP、ASP.NET、JSP、HP专用类库组件,提高开发效率
 
立即试用ATGrid报表控件
 

 

示例演示&源码下载
 

 

ATGrid报表控件功能
1、设计报表式样,生成模板文件
专用报表模板设计器,可视化设计报表式样
 
ATGrid报表控件有专门的设计器——wintable报表设计器,用来设计报表模板。wintable采用所见即所得的设计模式,类Excel操作界面风格,易学易用。可以直接导入Excel电子表格文件,也可以把ATGrid报表导出成Excel文件、html文件、xml文件。
 

支持分组、交叉、分栏等复杂报表
 
ATGrid报表控件采用表格方式构建报表,方便灵活,数据模型先进,可支持多个变长数据集,支持横纵向扩展,支持分组、交叉、分栏等复杂报表。
 

支持23种图表展现方式
 
ATGrid报表控件支持饼图、折线图、柱状图等23种图表展现方式,在一张报表中既可以有报表又可以有图表,大大丰富了报表数据的展现方式,增加了报表的可读性。
 

支持按钮、下拉列表、日期、多行文本等多种控件效果,支持会计表头、表览等多种特殊效果
 
ATGrid报表控件提供了33种单元格数据类型,包括数字、文本、密码、按钮、日期、列表等等,还包括会计表头、表览等多种特殊类型。使用ATGrid可以展现复杂的报表式样。
 

2、数据操作
拥有规范的xml数据格式
 
ATGrid报表控件提供xml数据接口,支持符合ATGrid数据格式规范的xml数据。ATGrid报表控件数据xml格式兼容ETcell报表控件数据格式。
 

内置通讯接口,强力支持HTTP协议,可以和后台程序轻松交互
 
ATGrid报表控件提供内置通讯接口,可以以post或get方式发送表单内数据或xml数据,可以接收返回的数据。为报表数据与其他程序交互操作提供了良好的支持,实现了数据与式样的分离。
 

采用Alias别名技术,可单独对单元格数据进行操作
 
ATGrid报表控件可以通过别名对单元格数据进行操作,大大增加了程序的灵活性和可扩展性。
 

数据模型先进,支持两种数据集——AliasSet和ETSet
 
ATGrid报表控件支持两种数据集模型,一种是AliasSet,相当于一条记录;一种是ETSet,相当于多条记录集。在ATGrid报表控件中可以直接对数据集进行操作,提高了编程的方便性。
 

ATGrid报表控件支持公式
 
ATGrid报表控件支持和Excel兼容的公式,单元格间可以自动进行计算。
 

3、编程接口控制
支持多种开发语言
 
支持多种常见开发语言,如JSP、ASP、ASP.NET、C#、HP、VB、VC++、Delphi、C++Builder、owerBuilder、Java等。
 

支持VBScript、JavaScript脚本
 
使用JavaScript或VBScript进行web编程,可以轻松实现浏览器端人机交互效果。
 

对象化编程
 
ATGrid是一款完全面向对象的报表控件,符合面向对象编程思想,支持7个子对象,最小对象到单元格,大大方便了面向对象编程。
 

提供数百个开发接口
 
ATGrid提供数百个开发接口,可以灵活的对式样、数据、事件进行控制。
 

4、打印输出
打印设置
 
可以按照打印效果的需要进行灵活的设置,包括页边距、纸张大小、打印方向、打印预览界面自定义、打印比例大小、表格线是否打印、是否自适应纸张、是否打印报表背景颜色、是否打印单元格背景,是否分页打印。
 

报表套打
 
可以按照预定格式,设置报表表格线是否打印、单元格是否打印,满足套打需要。
 

分页打印
 
可以设置固定表头等多种特殊效果,当数据过多超出一页纸时,可以进行分页打印。
 

自适应纸张打印
 
可以让报表自动缩放到符合纸张大小,充满整张纸,自动适应纸张进行打印。
 

导出其它格式文件
 
ATGrid报表可以导出Excel、html、xml等多种文件格式。导出为Excel文件时,报表内的公式、数据、式样会自动随着导出。
 
下载地址:
http://www.etcell.com/
http://www.etcell.com/download.aspx?id=51

GeneralNo items found from the Report expert Pinmembermasic_20014-Jul-07 7:10 
GeneralRe: No items found from the Report expert Pinmembermasic_20014-Jul-07 7:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 10 Apr 2007
Article Copyright 2007 by Prathapachandran.v
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid