简体   繁体   English

如何在C#Win Form应用程序中设置Sub Crystal报表的数据源

[英]How to set datasource of Sub crystal report in c# win form app

I am using this code to load a main report and a subreport inside the main report. 我正在使用此代码在主报告中加载主报告和子报告。 The main report is a blank one and just contains subreport. 主报告是空白报告,仅包含子报告。

Here is my code: 这是我的代码:

MySqlConnection cnn;
string connectionString = null;
string sql = null;

connectionString = "Server = BC; Database = mydb1; Uid = root; Pwd = abc123;";
cnn = new MySqlConnection(connectionString);
cnn.Open();

sql = "SELECT * from mytable1 ";
MySqlDataAdapter dscmd = new MySqlDataAdapter(sql, cnn);
DataSet1 ds = new DataSet1();
dscmd.Fill(ds, "Imagetest");
cnn.Close();

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("C:/Subreport.rpt");
cryRpt.SetDataSource(ds.Tables[1]);

crystalReportViewer1.ReportSource = "C:/MainReport.rpt";
crystalReportViewer1.Refresh();

When I run the application I just see main report with blank subreport. 当我运行该应用程序时,我只看到带有空白子报表的主报表。

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("C:/MainReport.rpt");
cryRpt.DataSourceConnections.Clear();
cryRpt.SetDataSource(ds.Tables[0]);
cryRpt.Subreports[0].DataSourceConnections.Clear();
cryRpt.Subreports[0].SetDataSource(ds.Tables[0]);
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();

Simply do this: 只需执行以下操作:

...
DataSet ds = new DataSet();
dscmd.Fill(ds, "DataTable1");
...

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("C:/MainReport.rpt");
cryRpt.SetDataSource(ds);

crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();

Note I'm creating a new DataSet and not a type derived from the DataSet. 注意我正在创建一个新的数据集,而不是从该数据集派生的类型。

In the report/subreport database fields menu, the table must have the same name as the DataTable: 在报表/子报表数据库字段菜单中,表必须与DataTable具有相同的名称:

数据库字段菜单中的表必须与数据表具有相同的名称

And the Datasource location for reports and subreports must be the same: 报表和子报表的数据源位置必须相同: 主报告的数据源位置

连接报告和子报告的路径相同

This will bind the source tables by name with the .rpt file. 这会将源表按名称与.rpt文件绑定。 Doing in this way you no longer need to set the data for each subreport by code. 这样,您不再需要通过代码为每个子报表设置数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM