轉(zhuǎn)帖|使用教程|編輯:莫成敏|2020-05-12 10:05:33.300|閱讀 367 次
概述:ActiveReports允許您在運(yùn)行時(shí)修改數(shù)據(jù)源。請(qǐng)參閱以下示例代碼集,以在運(yùn)行時(shí)將Page報(bào)表或RDL報(bào)表連接到數(shù)據(jù)源。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
ActiveReports 是一款專注于 .NET 平臺(tái)的報(bào)表控件,全面滿足 HTML5、WinForm、ASP.NET、.NET Core、WPF 等平臺(tái)下的中國式復(fù)雜報(bào)表設(shè)計(jì)和跨平臺(tái)報(bào)表開發(fā)需求,作為專業(yè)的報(bào)表工具為全球超過 300,000 名開發(fā)者提供全面的報(bào)表解決方案。
ActiveReports允許您在運(yùn)行時(shí)修改數(shù)據(jù)源。請(qǐng)參閱以下示例代碼集,以在運(yùn)行時(shí)將Page報(bào)表或RDL報(bào)表連接到數(shù)據(jù)源。
連接到OleDB數(shù)據(jù)源
使用API在運(yùn)行時(shí)在報(bào)表上設(shè)置數(shù)據(jù)源和數(shù)據(jù)集。這些步驟假定您已經(jīng)添加了頁面報(bào)表模板,并將Viewer控件放置在Visual Studio項(xiàng)目中的Windows窗體上。
注意:可以將以下代碼示例用于SQL,Odbc或OleDB數(shù)據(jù)源綁定。為此,請(qǐng)根據(jù)數(shù)據(jù)源修改數(shù)據(jù)提供者類型和連接字符串。
1、從Visual Studio工具箱中,將“表”數(shù)據(jù)區(qū)域拖放到報(bào)表的設(shè)計(jì)圖面上。
2、在表中,選擇以下單元格,然后轉(zhuǎn)到“屬性窗口”以設(shè)置其“值”屬性。
3、轉(zhuǎn)到“ Visual Studio報(bào)表”菜單,然后選擇“保存布局”。
單元格
值屬性
左單元格
=Fields!ProductID.Value
中間單元格
=Fields!InStock.Value
右單元格
=Fields!Price.Value
4、在出現(xiàn)的“另存為”窗口中,導(dǎo)航到項(xiàng)目的文件夾,然后將布局(如RuntimeBinding.rdlx)保存在bin / debug文件夾中。
5、雙擊Windows窗體的標(biāo)題欄,為Form_Load事件創(chuàng)建事件處理方法。
6、將以下代碼添加到處理程序中,以連接到數(shù)據(jù)源,添加數(shù)據(jù)集并在報(bào)表中提供數(shù)據(jù)。
Visual Basic.NET代碼粘貼到Form_Load事件中。
'create an empty page report Dim def As New PageReport 'load the report layout def.Load(New System.IO.FileInfo(Application.StartupPath + "\RuntimeBinding.rdlx")) 'create and setup the data source Dim myDataSource As New GrapeCity.ActiveReports.PageReportModel.DataSource myDataSource.Name = "Example Data Source" myDataSource.ConnectionProperties.DataProvider = "OLEDB" myDataSource.ConnectionProperties.ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[User folder]\Samples14\Data\Reels.mdb" 'setup the dataset Dim myDataSet As New GrapeCity.ActiveReports.PageReportModel.DataSet() Dim myQuery As New GrapeCity.ActiveReports.PageReportModel.Query() myDataSet.Name = "Example Data Set" myQuery.DataSourceName = "Example Data Source" myQuery.CommandType = GrapeCity.ActiveReports.PageReportModel.QueryCommandType.TableDirect myQuery.CommandText = GrapeCity.ActiveReports.Expressions.ExpressionInfo.FromString("Product") myDataSet.Query = myQuery ' add fields Dim _field As New GrapeCity.ActiveReports.PageReportModel.Field("ProductID", "ProductID", Nothing) myDataSet.Fields.Add(_field) _field = New GrapeCity.ActiveReports.PageReportModel.Field("InStock", "InStock", Nothing) myDataSet.Fields.Add(_field) _field = New GrapeCity.ActiveReports.PageReportModel.Field("Price", "Price", Nothing) myDataSet.Fields.Add(_field) 'bind the data source and the dataset to the report def.Report.DataSources.Add(myDataSource) def.Report.DataSets.Add(myDataSet) Viewer1.LoadDocument(def.Document)
C#代碼粘貼到Form_Load事件中。
//create an empty page report GrapeCity.ActiveReports.PageReport def = new GrapeCity.ActiveReports.PageReport(); //load the report layout def.Load(new System.IO.FileInfo(Application.StartupPath + "\RuntimeBinding.rdlx")); //create and setup the data source GrapeCity.ActiveReports.PageReportModel.DataSource myDataSource = new GrapeCity.ActiveReports.PageReportModel.DataSource(); myDataSource.Name = "Example Data Source"; myDataSource.ConnectionProperties.DataProvider = "OLEDB"; myDataSource.ConnectionProperties.ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[User folder]\\Samples14\\Data\\Reels.mdb"; //setup the dataset GrapeCity.ActiveReports.PageReportModel.DataSet myDataSet = new GrapeCity.ActiveReports.PageReportModel.DataSet(); GrapeCity.ActiveReports.PageReportModel.Query myQuery = new GrapeCity.ActiveReports.PageReportModel.Query(); myDataSet.Name = "Example Data Set"; myQuery.DataSourceName = "Example Data Source"; myQuery.CommandType = GrapeCity.ActiveReports.PageReportModel.QueryCommandType.TableDirect; myQuery.CommandText = GrapeCity.ActiveReports.Expressions.ExpressionInfo.FromString("Product"); myDataSet.Query = myQuery; // add fields GrapeCity.ActiveReports.PageReportModel.Field _field = new GrapeCity.ActiveReports.PageReportModel.Field("ProductID", "ProductID", null); myDataSet.Fields.Add(_field); _field = new GrapeCity.ActiveReports.PageReportModel.Field("InStock", "InStock", null); myDataSet.Fields.Add(_field); _field = new GrapeCity.ActiveReports.PageReportModel.Field("Price", "Price", null); myDataSet.Fields.Add(_field); //bind the data source and the dataset to the report def.Report.DataSources.Add(myDataSource); def.Report.DataSets.Add(myDataSet); def.Run(); viewer1.LoadDocument(def.Document);7、按F5鍵運(yùn)行該應(yīng)用程序。
連接到未綁定的數(shù)據(jù)源
要在運(yùn)行時(shí)連接到未綁定的數(shù)據(jù)源,可以將DataSet提供程序或Object提供程序與LocateDataSource事件一起使用。 當(dāng)報(bào)告引擎需要輸入數(shù)據(jù)以使用時(shí),報(bào)告引擎將引發(fā)LocateDataSource事件。
數(shù)據(jù)集提供者
使用DataSet提供程序,ConnectionString和Query設(shè)置會(huì)根據(jù)您連接數(shù)據(jù)的方式而有所不同。
要使用LocateDataSource事件將報(bào)表綁定到數(shù)據(jù),請(qǐng)將ConnectionString留空。
要將報(bào)表綁定到文件中的數(shù)據(jù)集,請(qǐng)將ConnectionString設(shè)置為文件的路徑,并將Query設(shè)置為DataSet表名。
數(shù)據(jù)集提供者的局限性
父表字段
要從父表中請(qǐng)求字段,請(qǐng)?jiān)谧侄蚊Q前添加必須遍歷的關(guān)系名稱才能導(dǎo)航到適當(dāng)?shù)母副怼?字段名稱和與句點(diǎn)的關(guān)系要分開。
例如,考慮一個(gè)名為OrderDetails的主表,它具有一個(gè)名為Orders的父表。 名為Orders_OrderDetails的關(guān)系定義了兩個(gè)表之間的關(guān)系。 使用具有以下語法的字段從父表訪問OrderDate:
Orders_OrderDetails.OrderDate
使用相同的技術(shù)遍歷表關(guān)系的多個(gè)級(jí)別。 例如,考慮在先前示例中使用的Orders表具有一個(gè)名為Customers的父表,以及一個(gè)將這兩個(gè)表綁定在一起的關(guān)系,稱為Customers_Orders。 如果CommandText將主表指定為OrderDetails,請(qǐng)使用以下語法從父表獲取CustomerName字段:
Customers_Orders.Orders_OrderDetails.CustomerName
注意:如果字段和關(guān)系具有相同的名稱,則可能會(huì)出現(xiàn)歧義。 不支持。
使用數(shù)據(jù)集提供程序
您可以使用API在運(yùn)行時(shí)在報(bào)表上設(shè)置數(shù)據(jù)集。
數(shù)據(jù)集提供程序返回一個(gè)數(shù)據(jù)表。 數(shù)據(jù)表中的所有字段均可用。 要將數(shù)據(jù)集提供程序用作報(bào)表的數(shù)據(jù)源,請(qǐng)?jiān)O(shè)置報(bào)表定義和運(yùn)行時(shí),然后將頁面文檔附加到LocateDataSourceEventHandler。
這些步驟假定您已經(jīng)添加了頁面報(bào)表模板,并將Viewer控件放置在Visual Studio項(xiàng)目中的Windows窗體上。
1、在報(bào)表資源管理器中,轉(zhuǎn)到“數(shù)據(jù)源”節(jié)點(diǎn),然后右鍵單擊以選擇“添加數(shù)據(jù)源”。
2、在出現(xiàn)的“報(bào)表數(shù)據(jù)源”對(duì)話框中,將“類型”設(shè)置為DataSetProvider并關(guān)閉對(duì)話框。數(shù)據(jù)源節(jié)點(diǎn)出現(xiàn)在ReportExplorer中。
3、右鍵單擊數(shù)據(jù)源節(jié)點(diǎn),然后選擇添加數(shù)據(jù)集。
4、在出現(xiàn)的“數(shù)據(jù)集”對(duì)話框中,選擇“字段”頁面。
5、在“字段”頁面上,添加一個(gè)字段,例如= Fields!ProductID.Value和= Fields!InStock.Value。
6、單擊“確定”關(guān)閉對(duì)話框。具有字段名稱的節(jié)點(diǎn)出現(xiàn)在數(shù)據(jù)集名稱下方。
7、從Visual Studio工具箱的ActiveReports 14 Page Report選項(xiàng)卡中,將Table數(shù)據(jù)區(qū)域拖到報(bào)表的設(shè)計(jì)圖面上。
8、在ReportExplorer中,將新添加的字段添加到表的詳細(xì)信息行中的單元格上,并保存報(bào)告。
9、在Visual Studio解決方案資源管理器中,右鍵單擊YourProjectName,然后選擇“添加”>“類”。
10、在出現(xiàn)的“添加新項(xiàng)”窗口中,將該類重命名為DataLayer.cs或.vb,然后單擊“添加”。
11、在解決方案資源管理器中,雙擊DataLayer.cs或.vb以打開該類的代碼視圖,并將以下代碼粘貼到該類中。
Visual Basic.NET代碼粘貼到DataLayer類中。
Imports GrapeCity.ActiveReports.Expressions.ExpressionObjectModel Imports System.Globalization Imports System.Data.OleDb Friend NotInheritable Class DataLayer Private _datasetData As System.Data.DataSet Public Sub New() LoadDataToDataSet() End Sub Public ReadOnly Property DataSetData() As System.Data.DataSet Get Return _datasetData End Get End Property Private Sub LoadDataToDataSet() Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False; Data Source=[User folder]\\Samples14\\Data\\Reels.mdb" Dim productSql As String = "SELECT top 100 * FROM Product" _datasetData = New DataSet() Dim conn As New OleDbConnection(connStr) Dim cmd As OleDbCommand = Nothing Dim adapter As New OleDbDataAdapter cmd = New OleDbCommand(productSql, conn) adapter.SelectCommand = cmd adapter.Fill(_datasetData, "Products") End Sub End Class
C#代碼粘貼到DataLayer類中。
using System; using System.Data; using System.Data.OleDb; internal sealed class DataLayer { private DataSet dataSetData; public DataLayer() { LoadDataToDataSet(); } public DataSet DataSetData { get { return dataSetData; } } private void LoadDataToDataSet() { string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False; Data Source=[User folder]\\Samples14\\Data\\Reels.mdb"; string productSql = "SELECT * From Product"; dataSetData = new DataSet(); OleDbConnection conn = new OleDbConnection(connStr); OleDbCommand cmd = new OleDbCommand(productSql, conn); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dataSetData, "Products"); } }
注意:DataSetDataSource示例提供了有關(guān)如何創(chuàng)建DataLayer類的上下文,以下代碼中使用了該類。 可以從GitHub下載DataSetDataSource示例。 請(qǐng)參閱此處的示例說明。
12、雙擊Windows窗體的標(biāo)題欄,為Form_Load事件創(chuàng)建事件處理方法,然后將以下代碼添加到處理程序中。
Visual Basic.NET代碼粘貼到Form_Load事件中。
LoadReport()
Visual Basic.NET代碼將INSIDE粘貼在表單的類聲明中。
Dim WithEvents runtime As GrapeCity.ActiveReports.Document.PageDocument Private Sub LoadReport() Dim rptPath As New System.IO.FileInfo("..\..\YourReportName.rdlx") 'Create a report definition that loads an existing report. Dim definition As New GrapeCity.ActiveReports.PageReport(rptPath) 'Load the report definition into a new page document. runtime = New GrapeCity.ActiveReports.Document.PageDocument(definition) 'Attach the runtime to an event. This line of code creates the event shell below. Viewer1.LoadDocument(runtime) End Sub 'ActiveReports raises this event when it cannot locate a report's data source in the usual ways. Private Sub runtime_LocateDataSource(ByVal sender As Object, ByVal args As GrapeCity.ActiveReports.LocateDataSourceEventArgs) Handles Runtime.LocateDataSource Dim dl = New DataLayer args.Data = dl.DataSetData.Tables("Products") End Sub
C#代碼粘貼到Form_Load事件中。
LoadReport();
C#代碼將INSIDE粘貼在表單的類聲明中。
private void LoadReport() { System.IO.FileInfo rptPath = new System.IO.FileInfo("..\\..\\YourReportName.rdlx"); //Create a report definition that loads an existing report. GrapeCity.ActiveReports.PageReport definition = new GrapeCity.ActiveReports.PageReport(rptPath); //Load the report definition into a new page document. GrapeCity.ActiveReports.Document.PageDocument runtime = new GrapeCity.ActiveReports.Document.PageDocument(definition); //Attach the runtime to an event. This line of code creates the event shell below. runtime.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(runtime_LocateDataSource); viewer1.LoadDocument(runtime); } //ActiveReports raises this event when it cannot locate a report's data source in the usual ways. private void runtime_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args) { DataLayer dl = new DataLayer(); args.Data = dl.DataSetData.Tables["Products"]; }
對(duì)象提供者
使用API將報(bào)表數(shù)據(jù)源綁定到對(duì)象集合。要將對(duì)象提供程序綁定到報(bào)表,請(qǐng)?jiān)O(shè)置報(bào)表定義和頁面文檔,然后將頁面文檔附加到LocateDataSourceEventHandler。創(chuàng)建一個(gè)公共類,該公共類設(shè)置可以與數(shù)據(jù)字段綁定的屬性名稱。
對(duì)象提供者數(shù)據(jù)源必須具有查詢保留為空白且與對(duì)象提供者數(shù)據(jù)源的字段相對(duì)應(yīng)的字段的數(shù)據(jù)集。在“字段”下的“數(shù)據(jù)集”對(duì)話框中手動(dòng)添加這些字段。
使用對(duì)象提供程序時(shí),請(qǐng)始終將報(bào)表的ConnectionString留空,因?yàn)樗褂肔ocateDataSource事件綁定到對(duì)象。將查詢?cè)O(shè)置為以下值之一:
使用對(duì)象提供者
這些步驟假定您已經(jīng)添加了頁面報(bào)表模板,并將Viewer控件放置在Visual Studio項(xiàng)目中的Windows窗體上。
1、在報(bào)表資源管理器中,轉(zhuǎn)到“數(shù)據(jù)源”節(jié)點(diǎn),然后右鍵單擊以選擇“添加數(shù)據(jù)源”
2、在出現(xiàn)的“報(bào)表數(shù)據(jù)源”對(duì)話框中,將“類型”設(shè)置為ObjectProvider并關(guān)閉對(duì)話框。數(shù)據(jù)源節(jié)點(diǎn)出現(xiàn)在ReportExplorer中。
3、右鍵單擊數(shù)據(jù)源節(jié)點(diǎn),然后在出現(xiàn)的“數(shù)據(jù)集”對(duì)話框中選擇“字段”頁面。
4、在“字段”頁面中,添加一個(gè)== Fields!name.Value之類的字段,然后單擊“確定”關(guān)閉對(duì)話框。具有字段名稱的節(jié)點(diǎn)將出現(xiàn)在數(shù)據(jù)集名稱下方。
5、從Visual Studio工具箱的ActiveReports 14 Page Report選項(xiàng)卡中,將Table數(shù)據(jù)區(qū)域拖到報(bào)表的設(shè)計(jì)圖面上。
6、在ReportExplorer中,將新添加的字段添加到表的詳細(xì)信息行中的單元格上。
7、將報(bào)告另存為DogReport.rdlx。
8、在解決方案資源管理器中,右鍵單擊表單,然后選擇查看代碼以打開代碼視圖。
9、在窗體的“代碼視圖”中,將以下代碼粘貼到類聲明中。
Visual Basic.NET代碼將INSIDE粘貼在表單的類聲明中。
' Create a class from which to call a property. Public Class dog Private _name As String Public Property name() As String Get Return _name End Get Set(ByVal value As String) _name = Value End Set End Property End Class ' Create an array to contain the data. Dim dogArray As System.Collections.ArrayList ' Create a method to populate the data array. Private Sub LoadData() dogArray = New System.Collections.ArrayList() Dim dog1 As New dog() dog1.name = "border collie" dogArray.Add(dog1) dog1 = New dog() dog1.name = "cocker spaniel" dogArray.Add(dog1) dog1 = New dog() dog1.name = "golden retriever" dogArray.Add(dog1) dog1 = New dog() dog1.name = "shar pei" dogArray.Add(dog1) End Sub
C#代碼將INSIDE粘貼在表單的類聲明中。
// Create a class from which to call a property. public class dog { private string _name; public string name { get { return _name; } set { _name = value; } } } // Create an array to contain the data. System.Collections.ArrayList dogArray; // Create a method to populate the data array. private void LoadData() { dogArray = new System.Collections.ArrayList(); dog dog1 = new dog(); dog1.name = "border collie"; dogArray.Add(dog1); dog1 = new dog(); dog1.name = "cocker spaniel"; dogArray.Add(dog1); dog1 = new dog(); dog1.name = "golden retriever"; dogArray.Add(dog1); dog1 = new dog(); dog1.name = "shar pei"; dogArray.Add(dog1); }
10、設(shè)置報(bào)告并為LocateDataSource事件添加處理程序。
Visual Basic.NET代碼粘貼到Form_Load事件中。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Create file info with a path to the report in your project. Dim fi As New System.IO.FileInfo("..\\..\\DogReport.rdlx") ' Create a report definition using the file info. Dim repDef As New GrapeCity.ActiveReports.PageReport(fi) ' Create a page document using the report definition. Dim runt As New GrapeCity.ActiveReports.Document.PageDocument(repDef) ' Create a LocateDataSource event for the runtime. AddHandler runt.LocateDataSource, AddressOf runt_LocateDataSource ' Display the report in the viewer. The title can be any text. Viewer1.LoadDocument(runt) End Sub
C#代碼粘貼到Form_Load事件中。
private void Form1_Load(object sender, EventArgs e) { // Create file info with a path to the report in your project. System.IO.FileInfo fi = new System.IO.FileInfo("..\\..\\DogReport.rdlx"); // Create a report definition using the file info. GrapeCity.ActiveReports.PageReport repDef = new GrapeCity.ActiveReports.PageReport(fi); // Create a page document using the report definition. GrapeCity.ActiveReports.Document.PageDocument runt = new GrapeCity.ActiveReports.Document.PageDocument(repDef); // Create a LocateDataSource event for the runtime. runt.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(runt_LocateDataSource); // Display the report in the viewer. The title can be any text. viewer1.LoadDocument(runt); }
11、使用LocateDataSource事件從對(duì)象加載數(shù)據(jù)。
Visual Basic.NET代碼將INSIDE粘貼在表單的類聲明中。
Private Sub runt_LocateDataSource(ByVal sender As Object, ByVal args As GrapeCity.ActiveReports.LocateDataSourceEventArgs) If dogArray Is Nothing Then LoadData() args.Data = dogArray End Sub
C#代碼將INSIDE粘貼在表單的類聲明中。
void runt_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args) { if (dogArray == null) { LoadData(); } args.Data = dogArray; }
12、按F5運(yùn)行該應(yīng)用程序。
相關(guān)內(nèi)容推薦:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: