帖子引用
http://www.cnblogs.com/babyt/archive/2009/04/08/1431328.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Windows.Forms;
using System.Data.OleDb;
namespace DyCrystalReportDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String tblName = comboBox1.Text ;
String connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Threading.Thread.GetDomain().BaseDirectory+ "bbtcrall.mdb" + ";";
if (tblName == "")
{
MessageBox.Show("请选择表名");
comboBox1.Focus();
return;
}
//打开数据库连接
DataTable dt1 = new DataTable();
DataTable dtx = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbConnection cn = new OleDbConnection(connstr);
//打开选择的表(注意进行错误保护)
//如果要实现任意列,只要更改此处的SQL为具体的字段即可
da = new OleDbDataAdapter("SELECT * From " + tblName, cn);
da.Fill(dt1);
//处理ds1
clsDyCrystalReportCore xCore = new clsDyCrystalReportCore();
dtx = xCore.dtx(dt1);
ReportDocument myReport = new ReportDocument();
string reportPath = System.Threading.Thread.GetDomain().BaseDirectory + "crystalreport1.rpt";
myReport.Load(reportPath);
//绑定数据集,注意,一个报表用一个数据集。
myReport.SetDataSource(dtx);
crystalReportViewer1.ReportSource = myReport;
crystalReportViewer1.RefreshReport();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add ("Test1_1");
comboBox1.Items.Add ("Test1_2");
}
}
}
本文介绍了一个使用 Crystal Reports 实现动态报表展示的应用实例。通过 C# 编程结合 OleDb 数据适配器从 Access 数据库读取数据,并利用自定义核心类处理数据表格,最终在 Windows Forms 应用中成功预览报表。

2万+

被折叠的 条评论
为什么被折叠?



