这是我用来调用RDLC报表的源代码,只要在程序中给reportViewer.LocalReport.ReportPath指定好相应的报表文件,用reportViewer.LocalReport.DataSources.Add指定好已经查出来的数据源,就可以正常显示了。不过要记得,数据源的名字要和你在RDLC报表文件中用的数据源的名字相同。 用这种方法,根本不会在RDLC文件中出现连接字符串之类的东西。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Reporting.WinForms;namespace Report { public partial class frmReport : Form { DataTable dt = new DataTable(); public frmReport(DataTable datatable) { InitializeComponent(); dt = datatable; } private void frmReport_Load(object sender, EventArgs e) { // Set Processing Mode reportViewer.ProcessingMode = ProcessingMode.Local; // Set RDL file reportViewer.LocalReport.ReportPath = "Report.rdlc"; // Supply a DataTable corresponding to each report dataset reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataTable", dt)); // Add the reportviewer to the form reportViewer.Dock = DockStyle.Fill; this.Controls.Add(reportViewer); // Process and render the report reportViewer.RefreshReport(); } } }
怎么弄?
用这种方法,根本不会在RDLC文件中出现连接字符串之类的东西。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;namespace Report
{
public partial class frmReport : Form
{
DataTable dt = new DataTable();
public frmReport(DataTable datatable)
{
InitializeComponent();
dt = datatable;
} private void frmReport_Load(object sender, EventArgs e)
{ // Set Processing Mode reportViewer.ProcessingMode = ProcessingMode.Local;
// Set RDL file reportViewer.LocalReport.ReportPath = "Report.rdlc"; // Supply a DataTable corresponding to each report dataset reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataTable", dt)); // Add the reportviewer to the form reportViewer.Dock = DockStyle.Fill;
this.Controls.Add(reportViewer); // Process and render the report reportViewer.RefreshReport();
}
}
}