我已經在應用程序中實際完成了此操作。但是,不要擔心要傳遞哪些參數,並且會超出我的報告類的構造函數對象。它只是一個「ReportViewer」的實例,作爲我工作的對象。
public partial class MyReport : Form
{
protected ReportViewer reportViewer = new ReportViewer();
public MyReport()
{ InitializeComponent(); }
然後,我將揭露一些公共方法,一個是需要不同的參數,根據需要每個報告,但如果通用的傳遞數據集與在它的一個或多個表的報告。然後我將動態循環的表,並添加到報表查看器控制
public Boolean GenerateCommonReport(DataSet oDS,
String NameOfReport, String[] SubReports)
{
// Set Processing Mode.
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.LocalReport.LoadReportDefinition(GetRDLC(NameOfReport));
// I've actually an extended version that includes subreports with an array
// of separate .RDLC file names in case the report is nested... if so, those
// would need to be added too.
if(! (SubReports == null))
{
// the hitch here, is that in my case, any sub-reports are named by the same
// name as the .RDLC in the report just in case thee's any object renaming
// when you add one sub-report into the main report. Such as when adding
// textboxes, it will create textbox1, textbox2, textbox3, etc... So, you
// may have to wiggle with this some to match the actual name of the sub-report
// object in your main report.
foreach(String ar in SubReports)
reportViewer.LocalReport.LoadSubreportDefinition(ar, GetRDLC(ar));
}
// Next load the dataset into the report before finally showing.
foreach (DataTable oTbl in oDS.Tables)
// likewise with the datasets. If you look at your RDLC of the schema's
// used, it will reference an outer Dataset name, then force an "_" before
// the actual table it uses WITHIN that dataset. Don't worry, internally
// it splits it up and finds correct correlation.
reportViewer.LocalReport.DataSources.Add(
new ReportDataSource(oDS.DataSetName + "_" + oTbl.TableName, oTbl));
// Finally, add the report viewer control to your displaying form control
reportViewer.Dock = DockStyle.Fill;
this.Controls.Add(reportViewer);
reportViewer.SetDisplayMode(DisplayMode.PrintLayout);
reportViewer.RefreshReport();
// This is actually showing your form as a modal dialog window to the user
this.ShowDialog();
}
// get embedded reports directly from this DLL/project
private Stream GetRDLC(String RptRDLC)
{
// Ex: we want the report "FirstReport" within the "MyReports.dll"
// assembly "MyReports.FirstReport.rdlc"
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
"MyReports." + RptRDLC + ".rdlc");
return stream;
}
}
現在,實際上調用這個和把它放在一起......
DataSet oDS = YourObject.HoweverYouQueryToGetADataset();
MyReport oRpt = new MyReport();
oRpt.GenerateCommonReport(oDS, "MyFirstReport", null);
我已經剝離了一堆其他驗證try/catch等,並從我的實際代碼中剝離了特定的對象引用,但下面的模板應該會給你一個巨大的啓動。
謝謝......我會給你的方式嘗試... – 2011-01-24 05:27:25