Setting the datasource for a Local Report - .NET & Report Viewer

前端 未结 3 1385
野趣味
野趣味 2020-11-29 06:42

I have created a custom control (a windows form with a report viewer). I have the following code to load a local report:

Contained in CustomReportViewer Clas

3条回答
  •  攒了一身酷
    2020-11-29 07:40

    You need to create a ReportDataSource, and set its Value property - e.g. DataTable and IEnumerables are supported sources

    As an example, and assuming that a method exists to return a DataSet, with a single DataTable matching the columns needed by your report:

    DataSet ds = SomeMethodToRetrieveDataSet(); // e.g. via DataAdapter
    // If your report needs parameters, they need to be set ...
    ReportParameter[] parameters = new ReportParameter[...];
    
    ReportDataSource reportDataSource = new ReportDataSource();
    // Must match the DataSource in the RDLC
    reportDataSource.Name = "ReportData"; 
    reportDataSource.Value = ds.Tables[0];
    
    // Add any parameters to the collection
    reportViewer1.LocalReport.SetParameters(parameters); 
    reportViewer1.LocalReport.DataSources.Add(reportDataSource);
    reportViewer1.DataBind();
    

    Note that it is often easier to just embed the RDLC into your assembly, rather than having to retain separate RDLC files. Do this by selecting the Build Action on the RDLC as Embedded Resource, and then you can set the ReportEmbeddedResource property:

    reportViewer1.LocalReport.ReportEmbeddedResource = 
                             "MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc";
    

    Note that the resource string must include the fully qualified name of the resource (including Assembly).

提交回复
热议问题