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

前端 未结 3 1391
野趣味
野趣味 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:33

    The key for me was as answered by StuartLC as above... with further clarification in that when he said it "Must match the DataSource in the RDLC".. it actually turned out to be the "DataSetName" element value re: DataSet1

    I went round and round because it is called "DataSource" so I kept using the DataSource element name but apparently in the rdl and rdlc file this really signifies the DataSetName. So keeping that in mind here is the code as borrowed from Stuart above with my own. Note the DataSetName element value:

            using (SqlConnection sqlConn = new SqlConnection(rvConnection))
            using (SqlDataAdapter da = new SqlDataAdapter(rvSQL, rvConnection))
            {
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];
    
                this.reportViewer1.Reset();
                this.reportViewer1.ProcessingMode = ProcessingMode.Local; 
                this.reportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "ssrsExport.rdlc";
                ReportDataSource reportDataSource = new ReportDataSource();
                // Must match the DataSet in the RDLC
                reportDataSource.Name = "DataSet1"; 
                reportDataSource.Value = ds.Tables[0];
                this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);   
                this.reportViewer1.RefreshReport();
            }
    

提交回复
热议问题