日期:2014-05-20 浏览次数:20810 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; namespace ReportDemo { public partial class FrmMain : Form { /// <summary> /// 全局水晶报表对象 /// </summary> ReportDocument myReport; /// <summary> /// 数据集作为水晶报表“骨架” /// </summary> DataSet1 crDataSet = new DataSet1(); public FrmMain() { InitializeComponent(); } /// <summary> /// 往“骨架”塞数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_Load(object sender, EventArgs e) { /* *说明:自增列无需另外设置值 * * * * * */ //主表数据 crDataSet.MainTable.AddMainTableRow("大类一"); crDataSet.MainTable.AddMainTableRow("大类二"); crDataSet.MainTable.AddMainTableRow("大类三"); //子表数据 crDataSet.SubTable.AddSubTableRow(1, "小类一"); crDataSet.SubTable.AddSubTableRow(1, "小类二"); crDataSet.SubTable.AddSubTableRow(2, "小类三"); crDataSet.SubTable.AddSubTableRow(2, "小类四"); crDataSet.SubTable.AddSubTableRow(2, "小类五"); crDataSet.SubTable.AddSubTableRow(2, "小类六"); crDataSet.SubTable.AddSubTableRow(3, "小类七"); crDataSet.SubTable.AddSubTableRow(3, "小类八"); crDataSet.SubTable.AddSubTableRow(3, "小类九"); } /// <summary> /// “Report”按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnReport_Click(object sender, EventArgs e) { myReport = new CrystalReport1(); //只需给主报表设定数据源,无需再为子报表设定数据 myReport.SetDataSource(crDataSet); crystalReportViewer1.ReportSource = myReport; } /// <summary> /// 窗体关闭前删除水晶报表产生的临时文件,避免过多临时文件导致“报表加载失败” /// </summary> protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); myReport.Dispose(); } } }