Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / Visual Basic
Article

A New .NET Reporting Way

Rate me:
Please Sign up or sign in to vote.
4.67/5 (50 votes)
29 Sep 2007CPOL3 min read 403.4K   17.5K   248   116
Looking for a free and simple way to design and add reports to your .NET application? Take a look at MyNeoReport library.
Sample Image - myneoreport_designer.jpg

Introduction

About three years ago, I started developing in .NET using a famous free editor and I needed to add some reports to my applications. Since I like to see what I'm going to print, just not to waste half a forest in paper only to adjust that text font, I started developing a little reporting library and a designer that uses it. By the way, my initial work was included into that famous free .NET editor, but I abandoned that project because I saw that it was badly designed (this is the doom of most first drafts), so it became a brand new project: MyNeoReport engine and designer, that let me design reports in a WYSIWYG way and, if I want, I now have total control on each the report objects from my application.

Background

Today, if you are an hobbyist you have also some Microsoft® free solutions to develop your application, but these entry level IDEs still don't have built-in reporting tools (and I also dislike the one included in professional ones). There isn't a free (good and working) solution out there that lets you manage printing in an easy way in managed code, so I decided to begin MyNeoReport and now, after three years, it is quite mature (ok, there are no hyper-advanced features, but boy... it is simple to use, well-organized and free).

Using the Code

In this article, I'll describe some code that you can use for managing the report content in your application. Note that you can access the report at runtime or you can define the report in the designer. Note that the full designer covers almost all the capabilities of the engine, and if you want, you can write your own designer (maybe better than mine).

The preview of the sample report is shown below:

Preview

The engine is composed of two libraries:

  1. NeoDataType.MyNeoReport.dll - The core of all
  2. NeoDataType.LiveCodeEngine.dll - The engine for scripting feature

These libraries must be distributed with your application, but you need to reference only NeoDataType.MyNeoReport.dll, since the other is just for support. The main namespace you need to include is NeoDataType.MyNeoReport:

C#
// c#
using NeoDataType.MyNeoReport;

VB.NET
' vb.net
Imports NeoDataType.MyNeoReport

Here you can find all the report entity classes. The most important ones are:

  1. Report, of course, the most important
  2. Page, that defines the page characteristics
  3. Section, that identifies a section (report and page headers and footers, details and groups)
  4. Label, Image, Shape, DataLabel, Dataimage, that are the base items that you can print

Report identifies the report document, it allows you to load and save a report to disk, it gives you some information such as how many pages the report will print, but, more important, it exposes the Page property.

Page describes the page size, orientation, measure unit, and, of course, the sections to be printed.

Section is a horizontal band that contains the items to be printed. Default sections are ReportHeader, PageHeader, Details, PageFooter, ReporFooter, but MyNeoReport also supports grouping sections.

In summary, the structure of a report is as follows:

Report
 +- Page
     +- Sections
         +-Items

Create a New Report and Add Items

C#
// c#
private void CreateANewReportSample()
{
    // Create a new report
    Report report = new Report();
    
    // Now you can set Page properties
    Page page = report.Page;
    
    // Set the page width to 5x8 inches
    page.Units = ScaleMode.Inches;
    page.Width = 5;
    page.Height = 8;
    
    // Add some items to print
    // - add a label that indicates the page number
    Label label = page.ReportHeader.AddLabel();
    
    // This label uses special functions as described here:
    // http://www.devbox4.net/?q=node/34
    label.Text = "Page {@PageNumber} of {@PageCount}";

    // - add an ellipse shape rotated by 20 degrees
    // This is another way to create and add an item
    Shape shape = new Shape();
    shape.ShapeType = ShapeType.Ellipse;
    shape.Angle = 20;
    page.ReportHeader.AddItem(shape);
    
    // If you use the PageControl to add designer capability
    // to your application, you need to assign the Page object
    // to it
    pageControl.SetPage(report.Page);
}

Define the Details Data Source

C#
// c#
private void SetDetailsDataSource()
{
    // Default datasource is OleDbDataSource that lets you connect to OleDb providers.
    // You can choose to pass data to a datasource changing it to a TableDataSource
    // with section.ChangeDatasourceType(DataSourceType.UseDataTable)
    OleDbDataSource dataSource = (OleDbDataSource)report.Page.Details.DataSource;
    dataSource.ConnectionString = myConnectionString;
    
    // Add a label and a data label bounded to the data
    // to the details section
    // - add a label that indicate the record number
    Label label = page.ReportHeader.AddLabel();
    
    // This label uses special functions as described here:
    // http://www.devbox4.net/?q=node/34
    label.Text = "Product {@RecordNumber} of {@RecordCount}";

    // - add a data label that indicates product name
    DataLabel datalabel = page.Details.AddDataLabel();
    // Move the data label to the right
    datalabel.X = label.X + label.Width;
    datalabel.DataField = "ProductName";
    
    // - add a data label that indicates product price
    datalabel2 = page.Details.AddDataLabel();
    // Move the data label to the right
    datalabel2.X = datalabel.X + datalabel.Width;
    datalabel2.DataField = "UnitPrice";
    // Set the text in case the data is NULL
    datalabel2.NullText = "Price undefined";
    // Set the format of the data, according to MSDN 
    // for the format strings
    datalabel2.Format = "#,###.00";
    
    // You can call the standard connection string wizard
    // using dataSource.ShowConnectionStringWizard();

}

Show the Preview and Print

C#
// c#
private void ShowThePreview()
{
    // Ok, now that you set all the report properties and items
    // you can preview and print it!
    
    // First of all, you need to choose the destination printer.
    // This is needed also for the preview, so it can be appropriate.
    report.ShowPrinterDialog();

    // Show the preview in a separate window.
    // Note that you can use the NeoDataType.MyNeoReport.PreviewControl
    // if you want a preview in your form.
    report.ShowPreview();
    
    // You can also print the report directly with
    // report.Print();
    
}

Points of Interest

I like MyNeoReport because its classes let you create a report (data bounded or not) without a designer, but just via your code because the classes give you total control over each aspect of the report and its components.

If you think that the built in items (Label, Image, Shape, DataLabel, DataImage) are too few, you must know that MyNe<code>oReport is extensible: you can write your own item by just inheriting from NeoDataType.MyNeoReport.ItemBase. You can look at the Designer code that is supplied to check all the abilities of the Report classes.

This is just a little introduction to the power of MyNeoReport library. For more information, documentation, support and newer releases, you can visit the site that supports me.

History

The MyNeoReport 1.4 is a big update, with some new enhancements, optimizations and bug fixes. This is described in the file upgradeinfo.txt contained in the download package.

License

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


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Standard printer Pin
pedro zegarra11-Mar-08 5:52
pedro zegarra11-Mar-08 5:52 
QuestionGroup in a Report Pin
TGris8-Sep-07 4:03
TGris8-Sep-07 4:03 
AnswerRe: Group in a Report Pin
Fabio Zanetta8-Sep-07 4:14
Fabio Zanetta8-Sep-07 4:14 
QuestionProperty not found Pin
TGris4-Sep-07 2:09
TGris4-Sep-07 2:09 
AnswerRe: Property not found Pin
Fabio Zanetta4-Sep-07 2:19
Fabio Zanetta4-Sep-07 2:19 
GeneralRe: Property not found Pin
TGris8-Sep-07 3:51
TGris8-Sep-07 3:51 
GeneralVB.NET 2005 Pin
mightymax4125-Mar-07 10:32
mightymax4125-Mar-07 10:32 
AnswerRe: VB.NET 2005 Pin
MBCDC26-Mar-08 10:24
MBCDC26-Mar-08 10:24 
GeneralFirebird Pin
fiShBoN315-Feb-07 21:00
fiShBoN315-Feb-07 21:00 
GeneralRe: Firebird Pin
Fabio Zanetta15-Feb-07 23:27
Fabio Zanetta15-Feb-07 23:27 
GeneralRe: Firebird Pin
fiShBoN316-Feb-07 14:38
fiShBoN316-Feb-07 14:38 
GeneralRe: Firebird Pin
fiShBoN316-Feb-07 14:56
fiShBoN316-Feb-07 14:56 
GeneralRe: Firebird Pin
Fabio Zanetta16-Feb-07 21:11
Fabio Zanetta16-Feb-07 21:11 
GeneralConnection Pin
Bruno S26-Sep-06 9:01
Bruno S26-Sep-06 9:01 
QuestionSource code? Pin
Marc Clifton14-Aug-06 8:22
mvaMarc Clifton14-Aug-06 8:22 
AnswerRe: Source code? [modified] Pin
Vertyg014-Aug-06 10:11
Vertyg014-Aug-06 10:11 
GeneralRe: Source code? Pin
Marc Clifton14-Aug-06 10:22
mvaMarc Clifton14-Aug-06 10:22 
GeneralRe: Source code? Pin
Nish Nishant14-Aug-06 11:12
sitebuilderNish Nishant14-Aug-06 11:12 
GeneralRe: Source code? Pin
Orcrist14-Aug-06 17:41
Orcrist14-Aug-06 17:41 
GeneralRe: Source code? Pin
Fabio Zanetta15-Aug-06 8:05
Fabio Zanetta15-Aug-06 8:05 
GeneralRe: Source code? Pin
robvon15-Aug-06 13:24
robvon15-Aug-06 13:24 
GeneralRe: Source code? Pin
Fabio Zanetta15-Aug-06 14:23
Fabio Zanetta15-Aug-06 14:23 
GeneralRe: Source code? Pin
Marc Clifton15-Aug-06 16:19
mvaMarc Clifton15-Aug-06 16:19 
GeneralRe: Source code? [modified] Pin
Fabio Zanetta15-Aug-06 22:02
Fabio Zanetta15-Aug-06 22:02 
GeneralRe: Source code? Pin
Marc Clifton16-Aug-06 0:52
mvaMarc Clifton16-Aug-06 0:52 

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.