在 WinForm 檢視 CrystalReport
首先在一個WinForm上放置CrystalReportViewer控制項
程式碼檢視如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System;
using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace App { public partial class frmPreviewRpt : Form { private ReportClass m_rpt = new ReportClass(); public frmPreviewRpt(ReportClass rpt, DataTable dt) { InitializeComponent(); m_rpt = rpt; if (dt != null) m_rpt.SetDataSource(dt); this.Name = rpt.ResourceName; } private void frmPreviewRpt_Shown(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; } private void frmPreviewRpt_Load(object sender, EventArgs e) { crystalReportViewer1.DisplayGroupTree = false; crystalReportViewer1.ReportSource = m_rpt; } } } |
使用方式如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int year = 2013;
int month = 5; RPT_Name myRpt = new RPT_Name(); DataTable dt = getDataTable(); //傳遞報表DataTable資料 myRpt.SetDataSource(dt); //若報表有定義參數,用以下方式給值 myRpt.SetParameterValue("myyear", year.ToString()); myRpt.SetParameterValue("mymonth", month.ToString()); frmPreviewRpt frm = new frmPreviewRpt(myRpt, null); frm.MdiParent = (frmMain)this.Parent.Parent; frm.Text = this.Text + "預覽"; frm.Show(); |