本文转自:http://msdn.microsoft.com/zh-tw/library/hh273267
若要在 WPF 應用程式中使用 ReportViewer 控制項,您需要將 ReportViewer 組件加入至專案,並且以程式設計方式設定該組件。
WPF 支援 Windows Form 控制項,但是 ReportViewer 控制項不對 WPF 提供任何自動組態,如同在 Windows Form 應用程式中一樣。
在 WPF 應用程式中,您會在 WindowsFormsHost 控制項內主控 ReportViewer 控制項。
WindowsFormsHost 控制項對呼叫程式碼要求完全信任,也就是 Windows 中 WPF 用戶端應用程式的信任層級。
WPF XAML Browser Applications Overview):‘>在 WPF 瀏覽器應用程式中,應用程式預設會以部分信任執行,而且您必須執行下列操作 (如需詳細資訊,請參閱 WPF XAML 瀏覽器應用程式概觀):
將 WPF 瀏覽器應用程式設定為在完全信任的環境下執行。
確定部署網站在用戶端瀏覽器中是 [近端內部網路] 或 [信任的網站區域]。
在本逐步解說中,您將建立一個 WPF 應用程式,此應用程式會使用 AdventureWorks2008R2 範例資料庫顯示本機報表。
Visual Studio Professional 或 Ultimate
Walkthrough: Installing the AdventureWorks Database).‘>AdventureWorks2008R2 資料庫的存取權限 (請參閱<逐步解說:安裝 AdventureWorks 資料庫>)。
網際網路存取 (用於 Bing 地圖)
File menu, point to New and select Project.‘>在 [檔案] 功能表中,指向 [開新檔案],再選取 [專案]。
New Project dialog, select the WPF Application project type, name your project, and click OK.‘>在 [新專案] 對話方塊中,選取 [WPF 應用程式] 專案類型,為您的專案命名,然後按一下 [確定]。
WindowsFormsHost control onto the design surface for MainWindow.xaml.‘>從 [工具箱] 將 [WindowsFormsHost] 控制項拖曳到 MainWindow.xaml 的設計介面上。 這樣會將 WindowsFormstHost 所需的組件加入至專案。
Solution Explorer, right-click your project and select Add Reference.‘>在 [方案總管] 中,以滑鼠右鍵按一下您的專案,然後選取 [加入參考]。
.NET tab of the Add Reference dialog log, select the Microsoft.ReportViewer.WinForms assembly, and click OK.‘>在 [加入參考] 對話方塊的 [.NET] 索引標籤中,選取 [Microsoft.ReportViewer.WinForms] 組件,然後按一下 [確定]。
在 XAML 檢視中,加入下列反白顯示的各行:
<Window x:Class="WpfReportApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms" Title="MainWindow" Height="350" Width="525"> <Grid> <WindowsFormsHost><rv:ReportViewer x:Name="_reportViewer"/></WindowsFormsHost> </Grid> </Window>
這樣會在 XAML 頁面中建立名為 _reportViewer 的 ReportViewer 執行個體。接著您將建立 RDLC 報表,您將在 ReportViewer 控制項中顯示該報表。
Add, and select New Item.‘>在 [方案總管] 中,以滑鼠右鍵按一下專案,指向 [加入],然後選取 [新項目]。
Add New Item dialog box, select Report Wizard, keep the default name, and click Add.‘>在 [加入新項目] 對話方塊中,選取 [報表精靈],保留預設名稱,然後按一下 [加入]。 此時會啟動 [報表精靈] 與 [資料來源組態精靈]。
Database, click Next, select Dataset, and click Next again.‘>在 [資料來源組態精靈] 中選取 [資料庫],按 [下一步],選取 [資料集],然後再次按 [下一步]。
Choose Your Data Connection page, click New Connection.‘>按一下 [選擇您的資料連接] 頁面上的 [新增連接]。 Choose Data Source dialog box, select Microsoft SQL Server and click Continue.‘>如果您看見 [選擇資料來源] 對話方塊,請選取 [Microsoft SQL Server] 並按一下 [繼續]。
Server name box, type the server name that hosts the AdventureWorks2008R2 database, then in Select or enter a database name, select AdventureWorks2008R2, and then click OK.‘>在 [伺服器名稱] 方塊中,輸入裝載 AdventureWorks2008R2 資料庫的伺服器名稱,然後在 [選取或輸入資料庫名稱] 中,選取 [AdventureWorks2008R2],再按一下 [確定]。
Next twice.‘>按 [下一步] 兩次。
Choose Your Database Objects page, expand the Tables node, select the checkbox for the SalesOrderDetail (Sales) table, and click Finish.‘>展開 [選擇您的資料庫物件] 頁面上的 [資料表] 節點,然後選取 [SalesOrderDetail (Sales)] 資料表的核取方塊,再按一下 [完成]。
AdventureWorks2008R2DataSet is now created in your project.‘>現在您的專案中已建立稱為 AdventureWorks2008R2DataSet 的 DataSet 物件。
Next.‘>在 [報表精靈] 中,按 [下一步]。 Arrange Fields page, drag all the available fields to the Values pane.‘>
Arrange Fields page, drag all the available fields to the Values pane.‘>在 [排列欄位] 頁面中,將所有可用的欄位拖曳到 [值] 窗格。
這樣會建立簡單的表格式資料表,用於顯示範例資料。
Next three times to close the Report Wizard.‘>接著按三次 [下一步],關閉 [報表精靈]。
接著,您會加入程式碼將 ReportViewer 指向您建立的新報表,以及從 AdventureWorks2008R2DataSet 將資料加入至 ReportViewer。
開啟 MainWindow.xaml.cs,並將反白顯示的行加入至 MainWindow() 建構函式:
將下列程式碼加入至類別定義中 MainWindow() 建構函式下方:
private bool _isReportViewerLoaded; private void ReportViewer_Load(object sender, EventArgs e) { if (!_isReportViewerLoaded) { Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource(); AdventureWorks2008R2DataSet dataset = new AdventureWorks2008R2DataSet(); dataset.BeginInit(); reportDataSource1.Name = "DataSet1"; //Name of the report dataset in our .RDLC file reportDataSource1.Value = dataset.SalesOrderDetail; this._reportViewer.LocalReport.DataSources.Add(reportDataSource1); this._reportViewer.LocalReport.ReportEmbeddedResource = "<VSProjectName>.Report1.rdlc"; dataset.EndInit(); //fill data into adventureWorksDataSet AdventureWorks2008R2DataSetTableAdapters.SalesOrderDetailTableAdapter salesOrderDetailTableAdapter = new AdventureWorks2008R2DataSetTableAdapters.SalesOrderDetailTableAdapter(); salesOrderDetailTableAdapter.ClearBeforeFill = true; salesOrderDetailTableAdapter.Fill(dataset.SalesOrderDetail); _reportViewer.RefreshReport(); _isReportViewerLoaded = true; } }
Debug menu, select Start Debugging to run the WPF application.‘>在 [偵錯] 功能表中選取 [開始偵錯],以執行 WPF 應用程式。
[转]逐步解說:在 WPF 應用程式中使用 ReportViewer 显示 rdlc
原文:http://www.cnblogs.com/freeliver54/p/3556111.html