您可以尝试程序化方法,
//Load Report Definition.
// From File.
ReportViewer1.LocalReport.ReportPath = "C:\\Report.rdlc";
//Load Report Data.
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", data));
//Refresh Control.
ReportViewer1.LocalReport.Refresh();
如果您有一个包含数据的 DataTable,您只需将其作为 DataSource 添加到 ReportViewer。“DataSet1”是当前加载到报表查看器中的报表中数据集的名称。
因此,如果您使用的是实体框架,它将是这样的。
//Create Connection.
Entities db = new Entities();
//Get the Data Using the query supplied (Where Entities.SomeObject is the Entity to retrieve data).
IQueryable<Object> data = db.CreateQuery<Object>("SELECT VALUE c FROM Entities.SomeObject AS c WHERE c.SomeValue> 0");
//Reset Control. Doesn't Usually work if this is skipped for some reason.
ReportViewer1.Reset();
//Load Report Definition.
// From File.
ReportViewer1.LocalReport.ReportPath = "C:\\Report.rdlc";
//Load Report Data.
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", data));
//Refresh Control.
ReportViewer1.LocalReport.Refresh();
同样适用于 DataTable。