Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

Report

Generation in C#
MIS 21 – Introduction to Applications Development
Management Reporting
 The regular provision of information to decision-
makers within an organization to support them in
their work
 Can have graphs, text and tables
 Can be generated regularly (e.g. weekly, monthly)
or ad hoc
 Adding reporting features in a program saves the
user time and effort in preparing the report
Example of a Report
Creating Reports in C#
 Can be done by
 Using ReportViewer (for Visual Studio Standard
Edition)
 Using third-party software (e.g. CrystalReports)
 Manually draw the report using System.Drawing
and System.Drawing.Printing
 Forour version of C#, we will manually
create our reports
Manually Creating Reports
 Advantages
 Free
 Flexible and customizable
 Disadvantages
 Complicated
 Each text and graphic needs to be drawn
 Takes time to program
 Solution
 Use third-party libraries
Generating a Report
 using ReportLibrary;
 Create an instance of ReportClass
 Assign
values to the Properties of the
ReportClass (e.g. report.ReportTitle)
 Createa PrintPreviewDialog and
associate the report to it
Sample Report Code
ReportClass report = new ReportClass();
report.SetReportSource(headers, data);
report.ReportTitle = "Sample Report";
report.ReportLogo = Image.FromFile("company_logo.jpg");
report.TitleFont = new Font("Arial", 24, FontStyle.Bold);
report.TitleFontColor = Brushes.Green;
report.ShowTableGrid = false;
report.ShowTotals = false;
report.IsLandscape = false;

PrintPreviewDialog printPreview = new PrintPreviewDialog();


printPreview.Document = report.ReportDocument;
printPreview.ShowDialog();
Sample Report Code
ReportClass report = new ReportClass();
report.SetReportSource(headers, data);
report.ReportTitle = "Sample Report";
report.ReportLogo = Image.FromFile("company_logo.jpg");
report.TitleFont = new Font("Arial", 24, FontStyle.Bold);
report.TitleFontColor = Brushes.Green;
report.ShowTableGrid = false; Create a ReportClass
report.ShowTotals = false; and assign the
report.IsLandscape = false; Properties

PrintPreviewDialog printPreview = new PrintPreviewDialog();


printPreview.Document = report.ReportDocument;
printPreview.ShowDialog();
Important Properties
 ReportTitle – title of the report
 ReportLogo– image of the logo of the
company using the report
Passing Data to the Report
 Use the method SetDataSource()
 Takes 2 arguments
 string[] headers
 Column headers of the report
 List<object[]> data
 Data to be displayed in the report
Sample Report Code
ReportClass report = new ReportClass();
report.SetReportSource(headers, data);
report.ReportTitle = "Sample Report";
report.ReportLogo = Image.FromFile("company_logo.jpg");
report.TitleFont = new Font("Arial", 24, FontStyle.Bold);
report.TitleFontColor = Brushes.Green;
report.ShowTableGrid = false; Create a ReportClass
report.ShowTotals = false; and assign the
report.IsLandscape = false; Properties
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = report.ReportDocument;
printPreview.ShowDialog();
Sample Report Code
ReportClass report = new ReportClass();
report.SetReportSource(headers, data);
report.ReportTitle = "Sample Report";
report.ReportLogo = Image.FromFile("company_logo.jpg");
report.TitleFont = new Font("Arial", 24, FontStyle.Bold);
report.TitleFontColor = Brushes.Green;
report.ShowTableGrid = false; Puts the report in a
report.ShowTotals = false; PrintPreviewDialog and
report.IsLandscape = false; opens the dialog

PrintPreviewDialog printPreview = new PrintPreviewDialog();


printPreview.Document = report.ReportDocument;
printPreview.ShowDialog();
More Properties: Fonts and Colors
 The font family, size, style and color can be
changed
 Applies to the title, table header and body
of the report

report.TitleFont = new Font("Arial", 24, FontStyle.Bold);


report.TitleFontColor = Brushes.Green;
More Properties: Alignment
 Alignment can also be changed
 StringAlignment.Near (Left)
 StringAlignment.Center
 StringAlignment.Far (Right)

 Applies to the report title, table header


titles and numeric texts
More Properties: Boolean Flags
 IsLandscape – orientation of the report
 ShowTableGrid – shows borders of table
 ShowTotals – adds a total row at the end of
table and sums the numeric columns
 ShowTitleFirstPageOnly – displays title at
the first page only
More Properties: Height and Width
 Height and width auto-adjusts by
default
 Appliesto the logo, title, table header,
ad table rows
 Uses integers

You might also like