Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / Visual Basic

Printing Reports in .NET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (70 votes)
26 Aug 2008CPOL11 min read 440.5K   15.6K   257  
Using the library presented, you can print reports from C# and other .NET languages
using System;
using System.Data;
using System.Drawing;
//using System.Windows.Forms;
using ReportPrinting;

namespace ReportDocumentTesting
{
	/// <summary>
	/// A sample of grades
	/// </summary>
	public class ReportTest13 : IReportMaker
	{

        public void MakeDocument(ReportDocument reportDocument)
        {
            TextStyle.ResetStyles();

            ReportBuilder builder = new ReportBuilder (reportDocument);
            builder.StartLinearLayout (Direction.Vertical);

            TextStyle.TableRow.MarginTop = 0.05f;
            TextStyle.TableRow.MarginBottom = 0.03f;
            TextStyle.TableHeader.MarginBottom = 0.03f;

            builder.AddPageHeader ("Science 101", String.Empty, DateTime.Now.ToLongDateString());
            builder.AddFormatExpression (typeof (float), DecimalFormatString);
            DataTable roster = GetRoster();
            DataView dv;
            if (roster == null)
            {
                dv = null;
                System.Windows.Forms.MessageBox.Show ("oops... there was a problem loading the grades xml file.");
            }
            else
            {
                dv = roster.DefaultView;
            }

            builder.DefaultTablePen = null; // turn of all lines that aren't explicitly enabled later
            builder.AddTable (dv, true);
            builder.Table.InnerPenHeaderBottom = reportDocument.NormalPen;
            builder.Table.InnerPenRow = new Pen (Color.Gray, reportDocument.ThinPen.Width);
            builder.Table.OuterPenBottom = new Pen (Color.Gray, reportDocument.ThinPen.Width);
            builder.Table.HorizontalAlignment = ReportPrinting.HorizontalAlignment.Center;
            
            builder.AddColumn ("Name", "Name", 2.5f, false, false);

            float width = 0.7f;
            foreach (DataColumn col in roster.Columns)
            {
                if (col.ColumnName.StartsWith (catPrefix))
                {
                    string catName = GetCatName (col.ColumnName);
                    builder.AddColumn (col, catName, width, false, false, HorizontalAlignment.Right);
                }
            }

            builder.AddColumn ("Average", "Avg", width, false, false, HorizontalAlignment.Right);

            builder.FinishLinearLayout ();

        }


        DataTable GetRoster()
        {
            DataSet ds = new DataSet();
            MainForm.GradesXml.Position = 0;
            ds.ReadXml (MainForm.GradesXml);
            if (ds.Tables.Count >= 1)
            {
                return ds.Tables[0];
            }
            return null;
        }

        const string catPrefix = "Cat_";


        string GetCatName (string colName)
        {
            string name = String.Empty;
            if (colName.StartsWith(catPrefix))
            {
                name = colName.Replace (catPrefix, String.Empty);
            }
            return name;
        }



        static int decimalPlacesDisplayed = 1;

        /// <summary>
        /// Gets or sets the number of decimal places displayed
        /// </summary>
        public static int DecimalDisplayed
        {
            get { return decimalPlacesDisplayed; }
            set { decimalPlacesDisplayed = value; }
        }

        /// <summary>
        /// Gets a format string for floating point numbers
        /// </summary>
        public static string DecimalFormatString
        {
            get { return "F" + DecimalDisplayed; }
        }



	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


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

Comments and Discussions