我在项目中添加报表rptorders.rdlc和rptproduct.rdlc分别作为主报表和子报表,为子报表也设置了参数,在数据库中建了两个表orders和product,在Form窗体中添加了reportViewer控件,代码如下
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private DataSet dsOrders;
private void frmMain_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“storageDataSet.orders”中。您可以根据需要移动或移除它。
this.ordersTableAdapter.Fill(this.storageDataSet.orders);
this.reportViewer1.LocalReport.SubreportProcessing += new Microsoft.Reporting.WinForms.SubreportProcessingEventHandler(SubReportProcessingEventHandler); this.dsOrders = new DataSet();
this.dsOrders.Tables.Add("orders");
this.dsOrders.Tables.Add("product");
SqlConnection con = new SqlConnection("Data Source=TLY;Initial Catalog=storage;Integrated Security=True"); SqlCommand cmd = new SqlCommand("select * from orders", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(this.dsOrders.Tables["orders"]);
adp.SelectCommand.CommandText = "select * from product";
adp.Fill(this.dsOrders.Tables["product"]);
this.reportViewer1.LocalReport.DataSources.Add(
new Microsoft.Reporting.WinForms.ReportDataSource("storageDataSet_orders",
this.dsOrders.Tables["orders"])); this.reportViewer1.RefreshReport();
} private void SubReportProcessingEventHandler(object sender, Microsoft.Reporting.WinForms.SubreportProcessingEventArgs e)
{
e.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("storageDataSet_product", this.dsOrders.Tables["product"])); }
但是总是只显示主报表内容,错误提示:子报表无法显示!
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private DataSet dsOrders;
private void frmMain_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“storageDataSet.orders”中。您可以根据需要移动或移除它。
this.ordersTableAdapter.Fill(this.storageDataSet.orders);
this.reportViewer1.LocalReport.SubreportProcessing += new Microsoft.Reporting.WinForms.SubreportProcessingEventHandler(SubReportProcessingEventHandler); this.dsOrders = new DataSet();
this.dsOrders.Tables.Add("orders");
this.dsOrders.Tables.Add("product");
SqlConnection con = new SqlConnection("Data Source=TLY;Initial Catalog=storage;Integrated Security=True"); SqlCommand cmd = new SqlCommand("select * from orders", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(this.dsOrders.Tables["orders"]);
adp.SelectCommand.CommandText = "select * from product";
adp.Fill(this.dsOrders.Tables["product"]);
this.reportViewer1.LocalReport.DataSources.Add(
new Microsoft.Reporting.WinForms.ReportDataSource("storageDataSet_orders",
this.dsOrders.Tables["orders"])); this.reportViewer1.RefreshReport();
} private void SubReportProcessingEventHandler(object sender, Microsoft.Reporting.WinForms.SubreportProcessingEventArgs e)
{
e.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("storageDataSet_product", this.dsOrders.Tables["product"])); }
但是总是只显示主报表内容,错误提示:子报表无法显示!
2、不需设置参数