水晶报表动态表表

本文介绍了一个使用 Crystal Reports 实现动态报表展示的应用实例。通过 C# 编程结合 OleDb 数据适配器从 Access 数据库读取数据,并利用自定义核心类处理数据表格,最终在 Windows Forms 应用中成功预览报表。

帖子引用

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");       
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值