How to run Dynamic Crystal Report using Crystal Report Viewer in VS.NET 2005

Some time we have multiple reports for the application and we need to load the report based on the user's selection. The following piece of code will explain how to achieve this.

using System; 

using System.Drawing; 

using System.Collections; 

using System.ComponentModel; 

using System.Windows.Forms; 

using System.Data; 

using System.IO;

 

namespace DyanamicReport 

{ 

          /// <summary>

           /// Summary description for Form1. 

          /// </summary> 

          public class Form1 : System.Windows.Forms.Form 

          { 

                   private System.Windows.Forms.ComboBox CboReport; 

                   private System.Windows.Forms.Panel panel1; 

                   private CrystalDecisions.Windows.Forms.CrystalReportViewer cptReports; 

 

                   /// <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.CboReport = new System.Windows.Forms.ComboBox(); 

                             this.cptReports = new CrystalDecisions.Windows.Forms.CrystalReportViewer(); 

                             this.panel1 = new System.Windows.Forms.Panel(); 

                             this.panel1.SuspendLayout(); 

                             this.SuspendLayout(); 

                             //  

                             // CboReport 

                             //  

                             this.CboReport.Location = new System.Drawing.Point(0, 0); 

                             this.CboReport.Name = "CboReport"; 

                             this.CboReport.Size = new System.Drawing.Size(496, 21); 

                             this.CboReport.TabIndex = 0; 

                             this.CboReport.SelectedIndexChanged += new System.EventHandler
                             (
this.CboReport_SelectedIndexChanged_1); 

                             //  

                             // cptReports 

                             //  

                             this.cptReports.ActiveViewIndex = -1; 

                             this.cptReports.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 

                             this.cptReports.Location = new System.Drawing.Point(4, 3); 

                             this.cptReports.Name = "cptReports"; 

                             this.cptReports.SelectionFormula = ""; 

                             this.cptReports.Size = new System.Drawing.Size(717, 367); 

                             this.cptReports.TabIndex = 1; 

                             this.cptReports.ViewTimeSelectionFormula = ""; 

                             //  

                             // panel1 

                             //  

                             this.panel1.Controls.Add(this.cptReports); 

                             this.panel1.Location = new System.Drawing.Point(8, 40); 

                             this.panel1.Name = "panel1"; 

                             this.panel1.Size = new System.Drawing.Size(724, 383); 

                             this.panel1.TabIndex = 2; 

                             //  

                             // Form1 

                             //  

                             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 

                             this.ClientSize = new System.Drawing.Size(744, 422); 

                             this.Controls.Add(this.panel1); 

                             this.Controls.Add(this.CboReport); 

                             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.panel1.ResumeLayout(false); 

                             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) 

                   { 

                             CboReport_Fill(); 

                   } 

                   private void CboReport_Fill() 

                   { 

                             //Create a temp table to hold the reports description and path 

                             DataTable dt = new DataTable (); 

                             dt.Columns.Add ("Description", System.Type.GetType("System.String")); 

                             dt.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"}; 

                             dt.Rows.Add (FirstRow); 

                             //Pickup all the crystal reports in the Reports subdirectory of the program 

                             string[] fileList = Directory.GetFiles (Directory.GetCurrentDirectory() + @"\Reports",
                                                    "*.rpt");
 

                             foreach (string item in fileList) 

                             { 

                                      //add all the friendly report names into the temp table 

                                      int startPt = item.ToString().LastIndexOf(@"\Reports"); 

                                      string[] rowData = {item.Substring(startPt + @"\Reports".Length + 1, item.ToString

                                                                ().Length - (startPt + @"\Reports".Length + 1)),item}; 

                                      dt.Rows.Add (rowData); 

                             } 

                             //assign the temp table to the combo box 

                             CboReport.DataSource = dt; 

                             CboReport.DisplayMember = "Description"; 

                             CboReport.ValueMember = "Path"; 

                   } 

                   private void CboReport_SelectedIndexChanged_1(object sender, System.EventArgs e) 

                   { 

                             //identify if the first combo box item is not selected 

                             switch (CboReport.SelectedIndex > 0) 

                             { 

                                      //if another line in the combo box is selected then view the report  

                                      case true: 

                                                cptReports.ReportSource = CboReport.SelectedValue ; 

                                                cptReports.Zoom (25); 

                                                break; 

                                                //if the first line in the combo box is selected then clear the report 

                                      case false: 

                                                cptReports.ReportSource = null; 

                                                break; 

                             } 

                             cptReports.Zoom(100); 

                   } 

          } 

}

 

OutPut:-

aru1.gif

Up Next
    Ebook Download
    View all
    Learn
    View all