Click here to Skip to main content
15,888,984 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 440K   15.6K   257  
Using the library presented, you can print reports from C# and other .NET languages
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.Data;
using System.IO;
using ReportPrinting;

namespace ReportDocumentTesting
{
	/// <summary>
	/// Summary description for MyPrintDialog.
	/// </summary>
	public class SamplePrintDialog2 : System.Windows.Forms.Form
	{
        public ReportPrinting.PrintControl PrintControls;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.Button btnOpen;
        private System.Windows.Forms.Label lblFile;
        private System.Windows.Forms.CheckedListBox checkedListBox1;

        private ReportDocument reportDocument;
        private SampleReportMaker2 reportMaker;
		private System.Windows.Forms.Label label1;

        const string defaultName = @"nwind.mdb";

		public SamplePrintDialog2()
		{
            //
			// Required for Windows Form Designer support
			//
			InitializeComponent();

            this.reportDocument = new ReportDocument();
            // Setup the document's settings
            reportDocument.DefaultPageSettings.Margins.Top = 50;
            reportDocument.DefaultPageSettings.Margins.Bottom = 50;
            this.reportDocument.DefaultPageSettings.Landscape = true;

            this.PrintControls.Document = reportDocument;

            reportMaker = new SampleReportMaker2();
            this.reportDocument.ReportMaker = reportMaker;

            if (File.Exists(defaultName))
            {
                OpenFile(defaultName);
            }

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.PrintControls = new ReportPrinting.PrintControl();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.btnOpen = new System.Windows.Forms.Button();
			this.lblFile = new System.Windows.Forms.Label();
			this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// PrintControls
			// 
			this.PrintControls.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.PrintControls.Document = null;
			this.PrintControls.Location = new System.Drawing.Point(136, 224);
			this.PrintControls.Name = "PrintControls";
			this.PrintControls.PrintInBackground = true;
			this.PrintControls.ShowStatusDialog = true;
			this.PrintControls.Size = new System.Drawing.Size(392, 40);
			this.PrintControls.TabIndex = 15;
			this.PrintControls.Printing += new System.EventHandler(this.printing_Handle);
			// 
			// btnOpen
			// 
			this.btnOpen.Location = new System.Drawing.Point(136, 24);
			this.btnOpen.Name = "btnOpen";
			this.btnOpen.TabIndex = 16;
			this.btnOpen.Text = "Open File";
			this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
			// 
			// lblFile
			// 
			this.lblFile.Location = new System.Drawing.Point(232, 24);
			this.lblFile.Name = "lblFile";
			this.lblFile.Size = new System.Drawing.Size(272, 23);
			this.lblFile.TabIndex = 17;
			this.lblFile.Text = "Filename: ";
			this.lblFile.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// checkedListBox1
			// 
			this.checkedListBox1.CheckOnClick = true;
			this.checkedListBox1.Location = new System.Drawing.Point(32, 80);
			this.checkedListBox1.Name = "checkedListBox1";
			this.checkedListBox1.ScrollAlwaysVisible = true;
			this.checkedListBox1.Size = new System.Drawing.Size(184, 109);
			this.checkedListBox1.TabIndex = 18;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 24);
			this.label1.Name = "label1";
			this.label1.TabIndex = 19;
			this.label1.Text = "Northwind DB:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// SamplePrintDialog2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(544, 278);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label1,
																		  this.checkedListBox1,
																		  this.lblFile,
																		  this.btnOpen,
																		  this.PrintControls});
			this.Name = "SamplePrintDialog2";
			this.Text = "MyPrintDialog";
			this.Load += new System.EventHandler(this.MyPrintDialog_Load);
			this.ResumeLayout(false);

		}
		#endregion




        private void MyPrintDialog_Load(object sender, System.EventArgs e)
        {

        }


        private void OpenFile (string filename)
        {
            this.lblFile.Text = filename;
            this.reportMaker.Filename = filename;
            this.reportMaker.readDb();

            // display a list of tables that were loaded
            this.checkedListBox1.Items.Clear();
            int index = 0;
            foreach (DataTable dt in this.reportMaker.MyDataSet.Tables)
            {
                this.checkedListBox1.Items.Add(dt.TableName);
                this.checkedListBox1.SetItemChecked(index, true);
                index++;
            }

        }


        private void btnOpen_Click(object sender, System.EventArgs e)
        {
            this.openFileDialog1.Filter = "Access files (*.mdb)|*.mdb|All files (*.*)|*.*";
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                OpenFile(this.openFileDialog1.FileName);
            }

        }


        private void printing_Handle(object sender, EventArgs e)
        {
            //this.reportMaker.numberOfRowsLimit = (int) this.numericUpDown1.Value;
            this.reportMaker.TablesToPrint.Clear();
            foreach (object tableName in this.checkedListBox1.CheckedItems)
            {
                this.reportMaker.TablesToPrint.Add(tableName.ToString());
            }
        }



	}
}

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