如何在c#win窗体应用程序中设置Sub Crystal报表的数据源

问题描述 投票:6回答:2

我正在使用此代码在主报告中加载主报告和子报告。主报告是空白报告,仅包含子报告。

这是我的代码:

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();

运行应用程序时,我只看到带有空白子报表的主报表。

c# crystal-reports
2个回答
12
投票

0
投票
... dscmd.Fill(ds, "DataTable1"); ... ReportDocument cryRpt = new ReportDocument(); cryRpt.Load("C:/MainReport.rpt"); cryRpt.SetDataSource(ds);

在报告/子报告数据库字段菜单中,表的名称必须与数据表的名称相同:

Table in Database Fields menu must have the same name as the DataTable

这将按名称将源表与.rpt文件绑定。通过这种方式,您不再需要通过代码为每个子报表设置数据。

© www.soinside.com 2019 - 2024. All rights reserved.