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
 

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
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRe: Are you really using XSD?memberPrathapachandran.v7 Aug '07 - 21:42 

XSD means XML Schema definition. The XSD will provide only the schema of all the tables and its relations. While we associate an XSD file to a crystal report, we only provides the schema of the tables to the crystal report. That means, the Table Name, Field Names and it types etc. This schema is stored as binary format in crystal report. For displaying the report, we have to give the same schema to the crystal report dynamically, and also data.

GeneralRe: Are you really using XSD?memberSoftEngi7 Aug '07 - 22:54 
Thanks Prathapachandran.
 
Regards,
Software Engineer
GeneralATGrid Report Controlmemberetcell30 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 expertmembermasic_20014 Jul '07 - 7:10 
Hi:
 
I have read your instructions on how to include crystal reports in an application in c#. I have gone as far as creating the dataset "dataset1.xsd" (with some dummy fields).
 
I have also created a blank Crystal report designer page "CrystalReport1.rpt" however, when I try to include the dataset via the Report Expert it sees the dataset but with no fields (i.e. no items found) and therefore it cannot add the table on the right side. Am I missing a DLL or some other file to make this work?
 
Thanks in advance.
 
masic_2000
GeneralRe: No items found from the Report expertmembermasic_20014 Jul '07 - 7:48 
I found my answer. My default settings were all off. I set them back to normal and everything seems to work.
 
Thanks.
GeneralRe: No items found from the Report expertmemberPrathapachandran.v4 Jul '07 - 17:38 
Ok well.
QuestionLoad Picture objects dynamically.....in crystal report.memberIshara_P4 Jul '07 - 2:07 
Im devloping a asp.net project in VS.NET 2000.
can i set a PictureObject dynamically in crystal report (Vs.net 2000) with C# ?
AnswerRe: Load Picture objects dynamically.....in crystal report.memberPrathapachandran.v4 Jul '07 - 17:35 
Dear Ishara,
You can set picture dynamically in crystal report. For that purpose, create a dataset with field named "Logo" of type base64Binary and click and drag that picture object to the crystal report. (You can position the picture object using code also).
 
Set the data to the report as follows.
step 1:
//----------------------------------------------------
// Reading an image file and convert it to byte array
//----------------------------------------------------
FileStream fs = File.OpenRead(@"C:\Logo.bmp");
int length = (int)fs.Length;
byte[] image = new byte[length];
fs.Read(image,0,length);
//----------------------------------------------------
//Setting data to crystal report
//----------------------------------------------------
DataTable demoTable = new DataTable("table1");
demoTable.Columns.Add("Logo",typeof(byte[]));
DataRow row = demoTable.NewRow();
row["Logo"] = image; // Setting image
demoTable.Rows.Add(row);
 
// Then set the data table to crystal report.
 
Regards,
Prathap
 

GeneralRe: Load Picture objects dynamically.....in crystal report.memberIshara_P4 Jul '07 - 20:12 
HI Prathap...
 
thanks for ur reply...but i need to clarify somethings...
 
should i need to store the Picture object in the database..?
GeneralRe: Load Picture objects dynamically.....in crystal report.memberPrathapachandran.v4 Jul '07 - 20:32 
Sure. I know only with SQL Server.
 
In SQL Server, we have a datatype image. You can directly store the byte array that I mentioned before to SQL server and can bind that table to Crystal Report .
GeneralRe: Load Picture objects dynamically.....in crystal report.memberIshara_P5 Jul '07 - 1:40 
Thanks Prathap...its working fine...Rose | [Rose]
QuestionRe: Load Picture objects dynamically.....in crystal report.memberIshara_P14 Aug '07 - 0:30 
HI Prathap..,
 
i came across a problem today when i export this dinamically loded image to PDF..
its .."Error detected by export DLL"..now report cannot export to PDF..
help me plss....
 

Ishara..
Questionusing crytal report in asp.netmembernvc847 Jun '07 - 16:28 
I create a table and input data into its.
i want use crytal report display data that table in page.
I use asp.net.
help me, please.
Thanks.

 
nvc84
AnswerRe: using crytal report in asp.netmemberPrathapachandran.v7 Jun '07 - 17:21 
Hi,
It is the same way that I had mentioned above. You can place the Crystal Report Web viewer to display report in Web forms. All others are same.

GeneralExactly what I needed, thank you!!! (-)memberFaizer17 May '07 - 14:29 
Big Grin | :-D
GeneralRe: Exactly what I needed, thank you!!! (-)memberPrathapachandran.v22 May '07 - 17:42 
Welcome Smile | :)
Generaladd sub detail sectionmemberYe Wint Aung8 May '07 - 17:47 
i want to add sub detail section in one main section.
 
that detail section sometime display of main section critiea.
 
plz ansewer me..
 
yi mon
GeneralRe: add sub detail sectionmemberPrathapachandran.v22 May '07 - 17:56 
U can divide the details sections in to multiple sections and can do anything you want or you can use group section for your requirement.
 
If u make the question more clear, then I will try for a detailed answer
 
Smile | :)
GeneralRead RTF format which contain text and imagememberWisnu Ulandaru2 May '07 - 23:17 
Dear all....
I have problem to use crystal report in vb .net 2005.
I try to read field from database (I am using SQL Server 7)
The datatype of the field is Text.
I fill the field with rtf format which contain text and image.
When i load data from crystal report, if the field contain text only, crystal report succcessfully to display.
But if the field contain text and image, crystal report can not display data. The display is only characters and numbers.
 
Please help me....
Please help me....
 
Thx
 

QuestionHow to pass SQL to crystal report in VB.NETmemberjohnlee8817 Apr '07 - 7:00 
The report was created in cr11 using multiple tables (table driven). VBcode re-assemble the sql (mainly revise the where/group) and pass it to crystal report which is invoked by the VB application (no crystal report server is used). Can I do it this way and how? Thanks a lot.
AnswerRe: How to pass SQL to crystal report in VB.NETmemberPrathapachandran.v17 Apr '07 - 18:54 
Could you please make it more clear?
GeneralIts a good one.memberVinu Nair12 Apr '07 - 18:18 
Hi pratap
 
Its a nice work. You are lucky that you are getting enough leisure in office to go in depth in Crystal reports and so. Please go in depth analysis of reporting tools and publish articles accordingly.
Any way good work.
 

 
vinu Nair
GeneralRe: Its a good one.memberPrathapachandran.v16 Apr '07 - 17:25 
Thanks Vinu.
Generalgood workmembersuresh00810 Apr '07 - 5:51 
Hi Prathapachandran.v this is suresh..
am working on C#.NET field can u send me ur mail id... i have soe doubts in c#..
Can u give me ur mailid...
 
My mail id is surezkumar.s@gmail.com
 
sureshkumaran

GeneralRe: good workmemberPrathapachandran.v10 Apr '07 - 18:20 
Thank you. I had send my email id.

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

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