Code Project Pick: A simple reporting tool or your .NET applications…

A new .Net reporting way
Click title for source at Codeproject.com…
Go here for download…
By Fabio Zanetta

Introduction
About three years ago I started develop 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 to don’t waste an half forest in paper only to adjust that text font, I started to develop a little reporting library and a designer that use it. By the way, my first work was included into that famous free .Net editor, but I abandoned that project because I saw that it was bad designed (this is the doom of most of first drafts), so I become a brand new project: MyNeoReport engine and designer, that let me design reports in a WYSIWYG way and, if I want, I have a total control on each the report object from my application.
Background
Today if you are an hobbiest 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 hate also the one included into professional ones). There is not a free (good and working) solution out there that let you manage printings in a 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 managing the report content in your application. Note that you can access to the report at runtime or you can define the report in the designer. Note that the full designer cover quite 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

The engine is composed by two libraries:
NeoDataType.MyNeoReport.dllthe core of all.NeoDataType.LiveCodeEngine.dllthe 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 of 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 are:
Report, of course, the most important.Page, that define the page characteristics.Section, that identifies a section (report and page headers and footers, details and groups)Label, Image, Shape, DataLabel, Dataimage, that are the base items that you can print.
Report identifies the report document, it allow you to load and save to disk a report, it tell you some information such as how many pages the report will print, but, more important, it expose the Page property.
Page describes the page size, orientation, measure unit, and, of course, the sections to be printed.
Section is an horizontal band that contains the items to be printed. Default sections are ReportHeader, PageHeader, Details, PageFooter, ReporFooter, but MyNeoReport support also grouping sections.
In summary, the structure of a report is
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 indicate 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 let 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 indicate 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 indicate 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 leave to you a 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 just inheriting from NeoDataType.MyNeoReport.ItemBase. You can look the Designer code that is supplied to chek all the abilities of the Report classes.
This is just a little intro to the power of MyNeoReport library. For more info, documentations, support and newer releases you can visit the site that support me.
About this entry
You’re currently reading “Code Project Pick: A simple reporting tool or your .NET applications…,” an entry on TECH NOTES
- Published:
- October 4, 2007 / 12:08 am
- Category:
- Code Project Picks
- Tags:
Comments are closed
Comments are currently closed on this entry.