設計步驟:
1、軟件環境:要求系統已安裝Visual Studio .Net 集成開發系統,僅裝有 .Net Framework SDk 不能實現本例效果,成為 .Net Framework SDK 沒有水晶報表控件。
2、新建一個 Visual C# 項目 Windows 應用程序,設置Form1的Text="水晶報表浏覽器",StartPosition=CenterScreen //程序開始出現在屏幕中央,其它屬性均保持默認;
3、從工具箱拖入一個CrystalReportVIEwer,一個Button,一個openFileDialog,到窗體。
整個設計器的窗體布局只需將button1置於窗體底邊中部,不再需要其它多余布局。
設置crystalReportVIEwer1的屬性如下:
this.crystalReportVIEwer1.Dock = System.Windows.Forms.DockStyle.Fill;
//停靠方式為充滿整個窗體
//展開+DockPadding
this.crystalReportVIEwer1.DockPadding.Bottom = 50;
//底部空出放置Button的區域
this.crystalReportVIEwer1.DockPadding.Left = 5;
this.crystalReportVIEwer1.DockPadding.Right = 5;
this.crystalReportVIEwer1.DockPadding.Top = 5;
this.crystalReportVIEwer1.ReportSource = null;
//先不載入報表資源
設置button1的屬性如下:
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
//與窗體的下邊保持固定
this.button1.Text = "打開報表";
openFileDialog1是打開文件的控件,設置其屬性如下:
this.openFileDialog1.Filter
= "Crystal Report (*.rpt)|*.rpt|所有文件(*.*)|*.*";
//提供打開文件對話框的文件類型,
默認類型就是此字符串的最前一種定義的類型
this.openFileDialog1.Title = "打開水晶報表";
//打開文件對話框的標題
布局效果圖如下:
4、雙擊button1,添加button1_Click點擊事件:
private void button1_Click(object sender, System.EventArgs e)
{
try
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
this.crystalReportVIEwer1.ReportSource = @openFileDialog1.FileName;
//加載水晶報表,將報表文件綁定到CrystalReportVIEw 控件;
}
catch(Exception error)
{
MessageBox.Show(error.ToString(),"錯誤");
}
}
5、OK!按Ctrl+F5運行吧。
可以浏覽你系統內現有的報表實例:
...\Program Files\Microsoft Visual Studio .Net\Crystal Reports\Samples\Reports\Feature Examples\Chart.rpt
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication10
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportVIEwer1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.crystalReportVIEwer1 = new
CrystalDecisions.Windows.Forms.CrystalReportVIEwer();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// crystalReportVIEwer1
//
this.crystalReportViewer1.ActiveVIEwIndex = -1;
this.crystalReportVIEwer1.Dock= System.Windows.Forms.DockStyle.Fill;
this.crystalReportVIEwer1.DockPadding.Bottom = 50;
this.crystalReportVIEwer1.DockPadding.Left = 5;
this.crystalReportVIEwer1.DockPadding.Right = 5;
this.crystalReportVIEwer1.DockPadding.Top = 5;
this.crystalReportViewer1.Name = "crystalReportVIEwer1";
this.crystalReportVIEwer1.ReportSource = null;
this.crystalReportVIEwer1.Size = new System.Drawing.Size(292, 273);
this.crystalReportVIEwer1.TabIndex = 0;
//
// button1
//
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button1.Location = new System.Drawing.Point(104, 240);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "打開報表";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// openFileDialog1
//
this.openFileDialog1.Filter = "Crystal Report (*.rpt)|*.rpt|所有文件(*.*)|*.*";
this.openFileDialog1.Title = "打開水晶報表";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClIEntSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.crystalReportVIEwer1});
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "水晶報表浏覽器";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
this.crystalReportVIEwer1.ReportSource = @openFileDialog1.FileName;
//加載水晶報表,將資源報表綁定到水晶報表查看器
}
catch(Exception error)
{
MessageBox.Show(error.ToString(),"錯誤"); //處理異常錯誤
}
}
}
}