문제

I have one reportviewer called reportviewer1 and two reports (report1.rdlc,report2.rdlc) i want to display them into the reportviwer1 by choosing one of them in combo box by using the code i tried the following code

 reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1"));

reportViewer1.LocalReport.ReportEmbeddedResource = "university_project.Report1.rdlc";

        reportViewer1.LocalReport.Refresh();

but it didn't work what is the correct format of the code?is there a missing parameters in the ReportDataSource?

도움이 되었습니까?

해결책

You need to specify where the records are coming from (fill a dataSet) and set the LocalPath of the reportViewer:

var dataSet = new DataSet();
using (var connection = new SqlConnection("ConnectionString")) 
{
    var sqlAdapter = new SqlDataAdapter("SELECT * FROM TABLE1",connection); // Get the records
    sqlAdapter.Fill(dataSet, "Table1");
}
ReportViewer1.Reset();
ReportViewer1.LocalReport.Path = "university_project.Report1.rdlc"; // Path to your report file            
var dataSource = new ReportDataSource("ReportDataSet_Name", dataSet.Tables[0]); // Specify report's dataset name and the records it use
ReportViewer1.LocalReport.DataSources.Clear(); // Clean the sources so you can use different datasources each time
ReportViewer1.LocalReport.DataSources.Add(datasource);
ReportViewer1.LocalReport.Refresh();

다른 팁

You need to pass DataTable too:

reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", myDataTableWithData));

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top