⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form2.cs

📁 简单CRYSTAL REPORT程序,自己也是刚刚下来看有很多不明白的地方,仅供参考,一起学习
💻 CS
字号:
//********************************************
//File Name:     	LogOnParameters.sln
//Created:       	December 19, 2002
//Author ID:     	HAN
//Purpose:       	This C# .NET sample Windows application
//       			demonstrates how to pass SQL log on information to a
//                  main report and a subreport.
//
//********************************************


using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace csharp_win_subreport_logon
{
	/// <summary>
	/// Summary description for Form2
	/// </summary>
	public class Form2 : System.Windows.Forms.Form
	{

		Sections crSections;
		ReportObjects crReportObjects;
		SubreportObject	crSubreportObject;

		CrystalReport1 crReportDocument;
		ReportDocument crSubreportDocument;

		Database crDatabase;
		Tables crTables;
		TableLogOnInfo crTableLogOnInfo;
		ConnectionInfo crConnectioninfo;

		private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
		/// <summary>
		/// Required designer variable
		/// </summary>
		private System.ComponentModel.Container components = null;


	    public Form2(string ServerName,string UserID,string Password,string DatabaseName)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

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

			//declare an instance of the report and the connectionInfo object

			crReportDocument = new CrystalReport1();
			crConnectioninfo = new ConnectionInfo();

			//pass the necessary parameters to the connectionInfo object
			crConnectioninfo.ServerName = ServerName;
			crConnectioninfo.UserID = UserID;
			crConnectioninfo.Password = Password;
			crConnectioninfo.DatabaseName = DatabaseName;
			
			//set up the database and tables objects to refer to the current report
			crDatabase = crReportDocument.Database;
			crTables = crDatabase.Tables;

			//loop through all the tables and pass in the connection info
			foreach(Table crTable in crTables)
			{
				crTableLogOnInfo = crTable.LogOnInfo;
				crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
				crTable.ApplyLogOnInfo(crTableLogOnInfo);
				// Note that if you wish to change Database (catalog) name as well as
				// the server, you must set new crTable.Location property as follows:
				// crTable.Location = "<new_database_name>.<owner>." + crTable.Name;
			}

			//set the crSections object to the current report's sections
			crSections = crReportDocument.ReportDefinition.Sections;

			//loop through all the sections to find all the report objects
			foreach(Section crSection in crSections)
			{
				crReportObjects = crSection.ReportObjects;
				//loop through all the report objects to find all the subreports
				foreach(ReportObject crReportObject in crReportObjects)
				{
					if (crReportObject.Kind == ReportObjectKind.SubreportObject)
					{
						//you will need to typecast the reportobject to a subreport 
						//object once you find it
						crSubreportObject = (SubreportObject)crReportObject;

						//open the subreport object
						crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);

						//set the database and tables objects to work with the subreport
						crDatabase = crSubreportDocument.Database;
						crTables = crDatabase.Tables;

						//loop through all the tables in the subreport and 
						//set up the connection info and apply it to the tables
						foreach(Table crTable in crTables)
						{
							crConnectioninfo.ServerName = ServerName;
							crConnectioninfo.DatabaseName = DatabaseName;
							crConnectioninfo.UserID = UserID;
							crConnectioninfo.Password = Password;
							
							crTableLogOnInfo = crTable.LogOnInfo;
							crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
							crTable.ApplyLogOnInfo(crTableLogOnInfo);
						}
					}
				}
			}

			//view the report
			crystalReportViewer1.ReportSource = crReportDocument;
			this.WindowState = FormWindowState.Maximized;
			//add any initialization after the 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.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
			this.SuspendLayout();
			// 
			// crystalReportViewer1
			// 
			this.crystalReportViewer1.ActiveViewIndex = -1;
			this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.crystalReportViewer1.Name = "crystalReportViewer1";
			this.crystalReportViewer1.ReportSource = null;
			this.crystalReportViewer1.Size = new System.Drawing.Size(292, 273);
			this.crystalReportViewer1.TabIndex = 0;
			// 
			// Form2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.crystalReportViewer1});
			this.Name = "Form2";
			this.Text = "Form2";
			this.ResumeLayout(false);

		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -