Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

Dynamic Crystal Reports Viewing

Rate me:
Please Sign up or sign in to vote.
2.54/5 (20 votes)
22 Apr 20042 min read 158.4K   6.9K   39  
An article on simplified Crystal Reporting using C#.NET.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;


namespace prpts
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private CrystalDecisions.Windows.Forms.CrystalReportViewer cRVMain;
		private System.Windows.Forms.ComboBox cBxRptList;
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
	
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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.cRVMain = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
			this.cBxRptList = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// cRVMain
			// 
			this.cRVMain.ActiveViewIndex = -1;
			this.cRVMain.Cursor = System.Windows.Forms.Cursors.Hand;
			this.cRVMain.DisplayGroupTree = false;
			this.cRVMain.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.cRVMain.Location = new System.Drawing.Point(0, 28);
			this.cRVMain.Name = "cRVMain";
			this.cRVMain.ReportSource = null;
			this.cRVMain.Size = new System.Drawing.Size(292, 232);
			this.cRVMain.TabIndex = 0;
			// 
			// cBxRptList
			// 
			this.cBxRptList.Dock = System.Windows.Forms.DockStyle.Top;
			this.cBxRptList.Location = new System.Drawing.Point(0, 0);
			this.cBxRptList.Name = "cBxRptList";
			this.cBxRptList.Size = new System.Drawing.Size(292, 24);
			this.cBxRptList.TabIndex = 1;
			this.cBxRptList.Text = "-- pick report from list --";
			this.cBxRptList.SelectedIndexChanged += new System.EventHandler(this.cBxRptList_SelectedIndexChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(292, 260);
			this.Controls.Add(this.cBxRptList);
			this.Controls.Add(this.cRVMain);
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Special Reports";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			//Populate the Combo Box with the report names
			cBxRptList_Fill();
		}

		private void cBxRptList_Fill()
		{
			//Create a temp table to hold the reports description and path
			DataTable cBxContents = new DataTable ();
			cBxContents.Columns.Add ("Description", System.Type.GetType("System.String"));
			cBxContents.Columns.Add ("Path",System.Type.GetType("System.String"));
			//Create the first row of the temp table to tell users what to do
			string[] FirstRow = {" -- select a report to view --", "-1"};
			cBxContents.Rows.Add (FirstRow);
			//Pickup all the crystal reports in the rpts subdirectory of the program
			string[] fileList =  Directory.GetFiles (Directory.GetCurrentDirectory() + @"\rpts", "*.rpt");
			foreach (string item in fileList)
			{
				//add all the friendly report names into the temp table
                int startPt = item.ToString().LastIndexOf(@"\rpts");
				string[] rowData = {item.Substring(startPt + @"\rpts".Length + 1, item.ToString ().Length - (startPt + @"\rpts".Length + 1)),item};
				cBxContents.Rows.Add (rowData);
			}
			//assign the temp table to the combo box
			cBxRptList.DataSource = cBxContents;
			cBxRptList.DisplayMember = "Description";
			cBxRptList.ValueMember = "Path";
		}

		private void cBxRptList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			//identify if the first combo box item is not selected
			switch (cBxRptList.SelectedIndex > 0)
			{
					//if another line in the combo box is selected then view the report 
                    case true:
						cRVMain.ReportSource = cBxRptList.SelectedValue ;
						cRVMain.Zoom (25);
						break;
					//if the first line in the combo box is selected then clear the report
					case false:
						cRVMain.ReportSource = null;
						break;
             }
        }
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Hi all,

I am a long-time coder. Since 8th grade with Apple and Commodore Pet! Now, I'm a computer consultant and software developer in new york, ny.

Comments and Discussions