如何在c#中将两个数据表加载到同一个Crystal报表?

时间:2013-06-18 10:31:27

标签: c# crystal-reports

private void btnPrint_Click(object sender, EventArgs e)
{    
   Report crt = new Report();    
   DataTable dt = new DataTable();    
   dt = dba.getToForPrint(txtTONumber.Text);    
   dt = dba.getOrderDatails(txtTONumber.Text);    
   crt.SetDataSource(dt);    
   crystalReportViewer1.ReportSource = crt;     
}

我必须调用两个方法来获取数据。 我创建了两个数据表,如Datatable和OrderDetails。但是不能在报告查看器中查看两个表数据,给我任何建议在reportviewer中播种两个数据表吗?

3 个答案:

答案 0 :(得分:2)

假设我们有两个数据源dt和dt1,我们在报告的两个表中都有字段,然后按以下方式分配两个数据源:

// rpt is the object of CrystalDecisions.CrystalReports.Engine.ReportDocument()
   rpt.Database.Tables[0].SetDataSource(dt);
   rpt.Database.Tables[1].SetDataSource(dt1);

我希望它会对你有所帮助。 :)

答案 1 :(得分:0)

您可以使用数据源,也可以在Crystal报表中使用两个子报表。将数据表分配给每个人作为“水晶” 例如

reportDocument.Load(this.MapPath("rptmainReport.rpt"));
reportDocument.OpenSubreport("rptSubReport1.rpt").SetDataSource(dt1);
reportDocument.OpenSubreport("rptSubReport2JNR.rpt").SetDataSource(dt2);

rptSubReport1和rptSubReport2是mainReport的子报告。所以你已经将数据源设置为子报告

OR

添加虚拟数据列并向其添加数据,或者根据视图注释,您可以添加数据源

如果有任何查询可以随意发表评论

答案 2 :(得分:0)

首先必须将Crystal Report设置为使用ADO.NET架构。 (见下图)。要生成模式,您只需创建一个数据集并创建表来填充它。填充后,添加表(使用表名到数据集),然后可以将模式导出到xml文件。

List<Tuple<string, string>> sqlQueries = new List<Tuple<string, string>>();

// The sql queries below shoudl match the same column names 
// you want to pull back from the database for yoru report
sqlQueries.Add(new Tuple<string, string>("Table1Name", "SELECT TOP 1 .... FROM ..."));
sqlQueries.Add(new Tuple<string, string>("SubReportName", "SELECT TOP 1 .... FROM ..."));
sqlQueries.Add(new Tuple<string, string>("SubReport2TableName", "SELECT TOP 1 .... FROM ..."));

SqlConnection connection = new SqlConnection(ConnectionString);
DataSet resultSet = new DataSet();

foreach (var tuple in sqlQueries)
{
    SqlDataAdapter adapter = new SqlDataAdapter(tuple.Item1, connection);
    DataTable schema = new DataTable();
    adapter.Fill(schema);
    schema.TableName = tuple.Item2;
    resultSet.Tables.Add(schema);

}

// write out the schema to a file
string path = Path.Combine("PATH_TO_DATASET_XML.xml");
using (var writer = File.CreateText(path))
{
    writer.Write(resultSet.GetXmlSchema().Replace(" encoding=\"utf-16\"", ""));
}

接下来,将其用作Crystal Reports中的数据源

enter image description here

最后,只需使用相同的xml文件填充报告数据:

DataSet reportData = new DataSet();
SqlConnection connection = new SqlConnection();
SqlDataAdapter reportAdapter = new SqlDataAdapter();
reportAdapter.SelectCommand = new SqlCommand();
reportAdapter.SelectCommand.Connection = connection;

reportData.ReadXml("PATH_TO_DATASET_XML.xml");

List<Tuple<string, string>> sqlQueries = new List<Tuple<string, string>>();

sqlQueries.Add(new Tuple<string, string>("Table1Name", "SELECT .... FROM ..."));
sqlQueries.Add(new Tuple<string, string>("SubReportName", "SELECT .... FROM ..."));
sqlQueries.Add(new Tuple<string, string>("SubReport2TableName", "SELECT .... FROM ..."));

reportData.EnforceConstraints = false;

foreach (var tuple in sqlQueries)
{                
    reportAdapter.SelectCommand.CommandText = tuple.Item1;
    try
    {
        reportAdapter.Fill(reportData, tuple.Item2.Trim());
    }
    catch (Exception ex)
    {
        // Handle your stuff
    }
}

using (var exportReport = new ReportDocument())
{
    exportReport.Load("PATH_TO_RPT_FILE.rpt");
    exportReport.SetDataSource(reportData);

    // export report to wherever you want
}