Click here to Skip to main content
11,431,829 members (61,940 online)
Click here to Skip to main content

A New .NET Reporting Way

, 29 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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#
using NeoDataType.MyNeoReport;

' 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#
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#
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#
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 MyNeoReport 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)

Share

About the Author

Fabio Zanetta
Web Developer
Italy Italy
No Biography provided

Comments and Discussions

 
QuestionInsert Page No. in Footer Pin
Member 107007561-Apr-14 6:09
memberMember 107007561-Apr-14 6:09 
QuestionWhere were you man? Pin
Member 1032848616-Feb-14 2:22
memberMember 1032848616-Feb-14 2:22 
Question.net for mono Pin
eujobi1-Feb-14 6:39
membereujobi1-Feb-14 6:39 
QuestionNeoDataType ?? Pin
Wrangly3-Apr-13 5:42
memberWrangly3-Apr-13 5:42 
QuestionNot able to use LoadFrom function Pin
Mihir J30-Mar-13 3:39
memberMihir J30-Mar-13 3:39 
GeneralRe: Not able to use LoadFrom function Pin
Member 1014336225-Oct-13 11:24
memberMember 1014336225-Oct-13 11:24 
Questionpagecontrol Pin
oujea26-Jan-13 14:45
memberoujea26-Jan-13 14:45 
GeneralMy vote of 5 Pin
kelvin.kong3-Sep-12 21:36
memberkelvin.kong3-Sep-12 21:36 
GeneralMy vote of 5 Pin
manoj kumar choubey5-Mar-12 2:59
membermanoj kumar choubey5-Mar-12 2:59 
QuestionPrinting Dialog Range Problem Pin
Mr.Jocker26-Feb-12 22:22
memberMr.Jocker26-Feb-12 22:22 
AnswerRe: Printing Dialog Range Problem Pin
sweet022018-Jul-12 21:00
membersweet022018-Jul-12 21:00 
GeneralMy vote of 5 Pin
Samir Fatah18-Jan-12 19:55
memberSamir Fatah18-Jan-12 19:55 
GeneralWhere to place the files Pin
SimpleGuyMu4-Jan-11 21:31
memberSimpleGuyMu4-Jan-11 21:31 
GeneralRe: Where to place the files Pin
Bkins20-Jan-11 12:01
memberBkins20-Jan-11 12:01 
NewsWebsite archive found at http://web.archive.org/web/20071014181939/www.devbox4.net/?q=myneoreport Pin
Vijayakumar .Y21-Jul-10 1:02
memberVijayakumar .Y21-Jul-10 1:02 
GeneralDocumentation Pin
ArtieBall7-Mar-10 12:51
memberArtieBall7-Mar-10 12:51 
GeneralRe: Documentation Pin
zain78626-Apr-10 3:55
memberzain78626-Apr-10 3:55 
GeneralRe: Documentation Pin
tiocan6-May-10 6:05
membertiocan6-May-10 6:05 
QuestionWill someone continue this project? Pin
elmoe010125-Aug-09 23:56
memberelmoe010125-Aug-09 23:56 
AnswerRe: Will someone continue this project? Pin
Bkins20-Jan-11 12:16
memberBkins20-Jan-11 12:16 
GeneralRe: Will someone continue this project? Pin
Member 228177110-Oct-11 10:57
memberMember 228177110-Oct-11 10:57 
GeneralRe: Will someone continue this project? Pin
Tilmann7-Nov-11 1:15
memberTilmann7-Nov-11 1:15 
GeneralRe: Will someone continue this project? Pin
Member 228177110-Nov-11 11:15
memberMember 228177110-Nov-11 11:15 
GeneralPassing a parameter Pin
docbiz5-Oct-08 11:38
memberdocbiz5-Oct-08 11:38 
GeneralPrint Pin
Member 31133768-Sep-08 8:45
memberMember 31133768-Sep-08 8:45 
QuestionIs this project still active [modified] Pin
cmdrcody24-Jul-08 5:42
membercmdrcody24-Jul-08 5:42 
AnswerRe: Is this project still active Pin
goltra18-Sep-08 9:21
membergoltra18-Sep-08 9:21 
QuestionUp to date dll [modified] Pin
john kininjjew16-Jul-08 23:58
memberjohn kininjjew16-Jul-08 23:58 
GeneralReport Samples Pin
Member 197760319-Jun-08 10:31
memberMember 197760319-Jun-08 10:31 
Questionsome example for before print code in objects? Pin
wlnaim29-May-08 20:13
memberwlnaim29-May-08 20:13 
GeneralCount Function Pin
pablleaf9-May-08 4:19
memberpablleaf9-May-08 4:19 
Generaldatasources Pin
Roberto_Rwk5-Apr-08 21:50
memberRoberto_Rwk5-Apr-08 21:50 
QuestionHow can I switch the scriptTextbox from C# to VB? [modified] Pin
MBCDC26-Mar-08 11:00
memberMBCDC26-Mar-08 11:00 
GeneralRe: How can I switch the scriptTextbox from C# to VB? Pin
kithe21-Apr-08 20:52
memberkithe21-Apr-08 20:52 
QuestionHow can I print all data? Pin
Fruhstuck25-Mar-08 15:39
memberFruhstuck25-Mar-08 15:39 
GeneralNQuery Pin
kianoosh soorani10-Nov-07 3:09
memberkianoosh soorani10-Nov-07 3:09 
GeneralBeforePrintCode skip for the last record. Pin
MOISJWang6-Nov-07 11:03
memberMOISJWang6-Nov-07 11:03 
GeneralRe: BeforePrintCode skip for the last record. Pin
ilqlo7-Feb-11 22:51
memberilqlo7-Feb-11 22:51 
GeneralShowPreview as virtual method Pin
dorwin1-Oct-07 7:39
memberdorwin1-Oct-07 7:39 
GeneralRightToLeft Have problem Pin
kianoosh soorani29-Sep-07 23:44
memberkianoosh soorani29-Sep-07 23:44 
GeneralExport To Word Needed Pin
kianoosh soorani26-Sep-07 3:29
memberkianoosh soorani26-Sep-07 3:29 
GeneralRe: Export To Word Needed Pin
Fabio Zanetta26-Sep-07 4:28
memberFabio Zanetta26-Sep-07 4:28 
GeneralAdd Export to (*.pdf) ,.. Pin
kianoosh soorani26-Sep-07 16:20
memberkianoosh soorani26-Sep-07 16:20 
GeneralRe: Add Export to (*.pdf) ,.. Pin
Fabio Zanetta29-Sep-07 8:32
memberFabio Zanetta29-Sep-07 8:32 
Generalparameter [modified] Pin
kianoosh soorani21-Sep-07 23:59
memberkianoosh soorani21-Sep-07 23:59 
GeneralRe: parameter Pin
Fabio Zanetta22-Sep-07 1:34
memberFabio Zanetta22-Sep-07 1:34 
GeneralRe: parameter Pin
kianoosh soorani22-Sep-07 1:37
memberkianoosh soorani22-Sep-07 1:37 
GeneralRe: parameter Pin
Fabio Zanetta22-Sep-07 2:47
memberFabio Zanetta22-Sep-07 2:47 
GeneralRe: parameter Pin
kianoosh soorani29-Sep-07 23:18
memberkianoosh soorani29-Sep-07 23:18 
Generalsample need for parameter Pin
kianoosh soorani29-Sep-07 23:29
memberkianoosh soorani29-Sep-07 23:29 

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 | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 29 Sep 2007
Article Copyright 2006 by Fabio Zanetta
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid