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

后端 未结 2 795
余生分开走
余生分开走 2021-01-18 12:32

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:

相关标签:
2条回答
  • 2021-01-18 13:20

    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:

    And the Datasource location for reports and subreports must be the same:

    This will bind the source tables by name with the .rpt file. Doing in this way you no longer need to set the data for each subreport by code.

    0 讨论(0)
  • 2021-01-18 13:26
    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();
    
    0 讨论(0)
提交回复
热议问题