Crystal Report - 将DataSource设置为多个子报表将不起作用(.NET)

时间:2015-07-06 12:52:16

标签: datatable crystal-reports dataset subreport

我是Crystal Reports的新手,在使用Visual Studio的CR时出现问题。

我设计的报告只包含两个子报告。主报告根本没有进一步的数据。我创建了一个带有两个表的DataSet(xsd) - 每个子报表一个。在Visual Studio中,我创建了两个子报表并添加了正确的表来插入数据字段。我没有在主报表中添加DataTable,因为它没有自己的数据。这是在Visual Studio中使用模板设计器完成的(不确定名称是否正确)

在代码中,我将一些虚拟数据添加到DataSet的两个表中,并将正确的表添加到相应的子报表中。然后,报表将显示在Crystal Report Viewer中。但是根本没有数据,我得到一个空页。

我已经确认DataTable正确地填充了我的虚拟数据,并且使用了正确的报告和子报告。

我做错了什么?请参阅下面的代码:

 DataSet1 ds1 = new DataSet1(); // my DataSet with 2 tables
            report.DataSourceConnections.Clear(); //report is the ReportDocument with the correct Report

            DataTable t1 = ds1.Tables.Add("DataTable1");
            t1.Columns.Add("Id", Type.GetType("System.String"));
            t1.Columns.Add("Feld1", Type.GetType("System.String"));

            DataTable t2 = ds1.Tables.Add("DataTable2");
            t2.Columns.Add("Id", Type.GetType("System.String"));
            t2.Columns.Add("Feld1", Type.GetType("System.String"));
            t2.Columns.Add("Feld2", Type.GetType("System.String"));

            DataRow r;
            int i = 0;
            for (i = 0; i <= 9; i++)
            {
                r = t1.NewRow();
                r["Id"] = i.ToString();
                r["Feld1"] = string.Format("Item1{0}", i);
                t1.Rows.Add(r);
            }

            DataRow r2;
            int i2 = 0;
            for (i2 = 0; i2 <= 9; i2++)
            {
                r2 = t2.NewRow();
                r2["Id"] = i2.ToString();
                r2["Feld1"] = string.Format("Item1{0}", i2);
                r2["Feld2"] = string.Format("Item2{0}", i2);
                t2.Rows.Add(r2);
            }

            report.Subreports[0].DataSourceConnections.Clear();
            report.Subreports[1].DataSourceConnections.Clear();
            report.Subreports[0].SetDataSource(ds1.Tables[0]);
            report.Subreports[1].SetDataSource(ds1.Tables[1]);

报告将导出为PDF文件:

report.ExportToDisk(ExportFormatType.PortableDocFormat, "D:\\test.pdf");

1 个答案:

答案 0 :(得分:0)

因此,根据上述评论,我了解最佳解决方案是将子报告移动到报告标题。