4,816
社区成员
发帖
与我相关
我的任务
分享
class clsDyCrystalReportCore
{
/// <summary>
/// 将传入的datatable转换成报表模板所需要的datatable
/// 数据全部转换为string
/// </summary>
/// <param name="dt">来源表</param>
/// <returns>报表模板所需要的datatable</returns>
public DataTable dtx(DataTable dt)
{
DataSet1.BigTatableDataTable dtx1 = new DataSet1.BigTatableDataTable();
object[] obj = new object[dt.Columns.Count];
//特别注意:所选择的表的列的数目需<=Bigtable的字段数目
//请自行填写保护代码
for (int i = 0; i <dt.Rows.Count ; i++)
{
dtx1.Rows.Add(dtx1.NewRow());
for (int j = 0; j < dt.Columns.Count ; j++)
{
dtx1.Rows[i][j] = dt.Rows[i][j].ToString();
}
}
return dtx1;
}
}
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");
}
}
}