Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C#
Article

Crystal Report Basics and Integration with DataSet

Rate me:
Please Sign up or sign in to vote.
4.75/5 (26 votes)
9 Apr 2007CPOL9 min read 1.3M   8.6K   102   50
This article gives a brief introduction about Crystal Reports and how to bind a dataset (.xsd) with a Crystal Report.

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, <city w:st="on"><place w:st="on">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 <city w:st="on"><place w:st="on">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 <city w:st="on"><place w:st="on">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 <city w:st="on"><place w:st="on">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.

C#
// 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)


Written By
Founder Appfabs
India India
- C#, WPF, WCF
- Java, J2EE, jersey, Spring MVC, Spring Boot, Spring Security, Hibernate, Spring Data
- NoSQL with Cassandra
- C, C++, Qt, OpenGL, Python
- SQL Server, Oracle, PostgreSQL
- Glassfish, Apache, Tomcat, NodeJS and IIS
- Hadoop, Flume, Spark
- Amazon Web Services (AWS) & Jenkins
- Big Data and Analytics

Comments and Discussions

 
QuestionSharing More links Pin
heemanshubhalla1-Aug-14 9:30
heemanshubhalla1-Aug-14 9:30 
QuestionGetting Crystal Login Prompt Pin
tallion31-Jan-14 11:28
tallion31-Jan-14 11:28 
Questionincrease width Pin
killomp11-May-13 7:22
killomp11-May-13 7:22 
QuestionTechnically well done Pin
Adam1212Bell24-Mar-13 13:18
Adam1212Bell24-Mar-13 13:18 
Questionwww.reportingsoftware.info Pin
hannahdreadz21-Sep-12 0:42
hannahdreadz21-Sep-12 0:42 
QuestionCrystalReportViewer not in my Toolbox Pin
Ken Than6-Jan-12 6:11
Ken Than6-Jan-12 6:11 
Questioncode of create a vb.net application Pin
PoojaPanchal6-Mar-11 19:48
PoojaPanchal6-Mar-11 19:48 
GeneralDisplay Table2 details After Table 1 Completes Pin
LouisLewis29-Jun-10 13:32
LouisLewis29-Jun-10 13:32 
Questioncrystal report Pin
vivek kumar11-Apr-09 4:09
vivek kumar11-Apr-09 4:09 
GeneralQuery on crystal report tool Pin
Bheema8426-Mar-09 13:34
Bheema8426-Mar-09 13:34 
GeneralBinding Object as datasource for crystal reports. Pin
Xandip18-Jan-09 22:07
Xandip18-Jan-09 22:07 
GeneralGreat Article Pin
viv281226-Apr-08 14:32
viv281226-Apr-08 14:32 
Questionhow to change width of page in crystal report ? Pin
az_farzaneh8-Jan-08 21:46
az_farzaneh8-Jan-08 21:46 
AnswerRe: how to change width of page in crystal report ? Pin
Asif_ku17-Sep-09 7:09
Asif_ku17-Sep-09 7:09 
Generalreport more than one table Pin
tornado19797-Dec-07 12:51
tornado19797-Dec-07 12:51 
Questionto display vertical result of a query in crystal report Pin
az_farzaneh28-Nov-07 0:03
az_farzaneh28-Nov-07 0:03 
AnswerRe: to display vertical result of a query in crystal report Pin
Prathapachandran28-Nov-07 0:16
professionalPrathapachandran28-Nov-07 0:16 
GeneralRe: to display vertical result of a query in crystal report Pin
az_farzaneh28-Nov-07 19:56
az_farzaneh28-Nov-07 19:56 
GeneralRe: to display vertical result of a query in crystal report Pin
Prathapachandran28-Nov-07 20:11
professionalPrathapachandran28-Nov-07 20:11 
QuestionWill you please tell me how to ceate Guntt Chart in it? Pin
uranus122523-Sep-07 22:50
uranus122523-Sep-07 22:50 
I want to ceate Guntt Chart in this crystal report,but I have no idea how to fulfil it!
Will you please tell me how I should do!
Greate thanks!
I am in Beijing,China!Welcome to Beijing next year for Olympic Games!Maybe I can procide abode for you!hehe.
Great thanks!
QuestionAre you really using XSD? Pin
SoftEngi7-Aug-07 21:01
SoftEngi7-Aug-07 21:01 
AnswerRe: Are you really using XSD? Pin
Prathapachandran7-Aug-07 21:42
professionalPrathapachandran7-Aug-07 21:42 
GeneralRe: Are you really using XSD? Pin
SoftEngi7-Aug-07 22:54
SoftEngi7-Aug-07 22:54 
GeneralATGrid Report Control Pin
etcell30-Jul-07 18:45
etcell30-Jul-07 18:45 
GeneralNo items found from the Report expert Pin
masic_20014-Jul-07 7:10
masic_20014-Jul-07 7:10 

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

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