轉(zhuǎn)帖|使用教程|編輯:莫成敏|2020-05-12 10:05:33.300|閱讀 367 次
概述:ActiveReports允許您在運行時修改數(shù)據(jù)源。請參閱以下示例代碼集,以在運行時將Page報表或RDL報表連接到數(shù)據(jù)源。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
ActiveReports 是一款專注于 .NET 平臺的報表控件,全面滿足 HTML5、WinForm、ASP.NET、.NET Core、WPF 等平臺下的中國式復(fù)雜報表設(shè)計和跨平臺報表開發(fā)需求,作為專業(yè)的報表工具為全球超過 300,000 名開發(fā)者提供全面的報表解決方案。
ActiveReports允許您在運行時修改數(shù)據(jù)源。請參閱以下示例代碼集,以在運行時將Page報表或RDL報表連接到數(shù)據(jù)源。
連接到OleDB數(shù)據(jù)源
使用API在運行時在報表上設(shè)置數(shù)據(jù)源和數(shù)據(jù)集。這些步驟假定您已經(jīng)添加了頁面報表模板,并將Viewer控件放置在Visual Studio項目中的Windows窗體上。
注意:可以將以下代碼示例用于SQL,Odbc或OleDB數(shù)據(jù)源綁定。為此,請根據(jù)數(shù)據(jù)源修改數(shù)據(jù)提供者類型和連接字符串。
1、從Visual Studio工具箱中,將“表”數(shù)據(jù)區(qū)域拖放到報表的設(shè)計圖面上。
2、在表中,選擇以下單元格,然后轉(zhuǎn)到“屬性窗口”以設(shè)置其“值”屬性。
3、轉(zhuǎn)到“ Visual Studio報表”菜單,然后選擇“保存布局”。
單元格
值屬性
左單元格
=Fields!ProductID.Value
中間單元格
=Fields!InStock.Value
右單元格
=Fields!Price.Value
4、在出現(xiàn)的“另存為”窗口中,導航到項目的文件夾,然后將布局(如RuntimeBinding.rdlx)保存在bin / debug文件夾中。
5、雙擊Windows窗體的標題欄,為Form_Load事件創(chuàng)建事件處理方法。
6、將以下代碼添加到處理程序中,以連接到數(shù)據(jù)源,添加數(shù)據(jù)集并在報表中提供數(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īng)用程序。
連接到未綁定的數(shù)據(jù)源
要在運行時連接到未綁定的數(shù)據(jù)源,可以將DataSet提供程序或Object提供程序與LocateDataSource事件一起使用。 當報告引擎需要輸入數(shù)據(jù)以使用時,報告引擎將引發(fā)LocateDataSource事件。
數(shù)據(jù)集提供者
使用DataSet提供程序,ConnectionString和Query設(shè)置會根據(jù)您連接數(shù)據(jù)的方式而有所不同。
要使用LocateDataSource事件將報表綁定到數(shù)據(jù),請將ConnectionString留空。
要將報表綁定到文件中的數(shù)據(jù)集,請將ConnectionString設(shè)置為文件的路徑,并將Query設(shè)置為DataSet表名。
數(shù)據(jù)集提供者的局限性
父表字段
要從父表中請求字段,請在字段名稱前添加必須遍歷的關(guān)系名稱才能導航到適當?shù)母副怼?字段名稱和與句點的關(guān)系要分開。
例如,考慮一個名為OrderDetails的主表,它具有一個名為Orders的父表。 名為Orders_OrderDetails的關(guān)系定義了兩個表之間的關(guān)系。 使用具有以下語法的字段從父表訪問OrderDate:
Orders_OrderDetails.OrderDate
使用相同的技術(shù)遍歷表關(guān)系的多個級別。 例如,考慮在先前示例中使用的Orders表具有一個名為Customers的父表,以及一個將這兩個表綁定在一起的關(guān)系,稱為Customers_Orders。 如果CommandText將主表指定為OrderDetails,請使用以下語法從父表獲取CustomerName字段:
Customers_Orders.Orders_OrderDetails.CustomerName
注意:如果字段和關(guān)系具有相同的名稱,則可能會出現(xiàn)歧義。 不支持。
使用數(shù)據(jù)集提供程序
您可以使用API在運行時在報表上設(shè)置數(shù)據(jù)集。
數(shù)據(jù)集提供程序返回一個數(shù)據(jù)表。 數(shù)據(jù)表中的所有字段均可用。 要將數(shù)據(jù)集提供程序用作報表的數(shù)據(jù)源,請設(shè)置報表定義和運行時,然后將頁面文檔附加到LocateDataSourceEventHandler。
這些步驟假定您已經(jīng)添加了頁面報表模板,并將Viewer控件放置在Visual Studio項目中的Windows窗體上。
1、在報表資源管理器中,轉(zhuǎn)到“數(shù)據(jù)源”節(jié)點,然后右鍵單擊以選擇“添加數(shù)據(jù)源”。
2、在出現(xiàn)的“報表數(shù)據(jù)源”對話框中,將“類型”設(shè)置為DataSetProvider并關(guān)閉對話框。數(shù)據(jù)源節(jié)點出現(xiàn)在ReportExplorer中。
3、右鍵單擊數(shù)據(jù)源節(jié)點,然后選擇添加數(shù)據(jù)集。
4、在出現(xiàn)的“數(shù)據(jù)集”對話框中,選擇“字段”頁面。
5、在“字段”頁面上,添加一個字段,例如= Fields!ProductID.Value和= Fields!InStock.Value。
6、單擊“確定”關(guān)閉對話框。具有字段名稱的節(jié)點出現(xiàn)在數(shù)據(jù)集名稱下方。
7、從Visual Studio工具箱的ActiveReports 14 Page Report選項卡中,將Table數(shù)據(jù)區(qū)域拖到報表的設(shè)計圖面上。
8、在ReportExplorer中,將新添加的字段添加到表的詳細信息行中的單元格上,并保存報告。
9、在Visual Studio解決方案資源管理器中,右鍵單擊YourProjectName,然后選擇“添加”>“類”。
10、在出現(xiàn)的“添加新項”窗口中,將該類重命名為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示例。 請參閱此處的示例說明。
12、雙擊Windows窗體的標題欄,為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"];
}
對象提供者
使用API將報表數(shù)據(jù)源綁定到對象集合。要將對象提供程序綁定到報表,請設(shè)置報表定義和頁面文檔,然后將頁面文檔附加到LocateDataSourceEventHandler。創(chuàng)建一個公共類,該公共類設(shè)置可以與數(shù)據(jù)字段綁定的屬性名稱。
對象提供者數(shù)據(jù)源必須具有查詢保留為空白且與對象提供者數(shù)據(jù)源的字段相對應(yīng)的字段的數(shù)據(jù)集。在“字段”下的“數(shù)據(jù)集”對話框中手動添加這些字段。
使用對象提供程序時,請始終將報表的ConnectionString留空,因為它使用LocateDataSource事件綁定到對象。將查詢設(shè)置為以下值之一:
使用對象提供者
這些步驟假定您已經(jīng)添加了頁面報表模板,并將Viewer控件放置在Visual Studio項目中的Windows窗體上。
1、在報表資源管理器中,轉(zhuǎn)到“數(shù)據(jù)源”節(jié)點,然后右鍵單擊以選擇“添加數(shù)據(jù)源”
2、在出現(xiàn)的“報表數(shù)據(jù)源”對話框中,將“類型”設(shè)置為ObjectProvider并關(guān)閉對話框。數(shù)據(jù)源節(jié)點出現(xiàn)在ReportExplorer中。
3、右鍵單擊數(shù)據(jù)源節(jié)點,然后在出現(xiàn)的“數(shù)據(jù)集”對話框中選擇“字段”頁面。
4、在“字段”頁面中,添加一個== Fields!name.Value之類的字段,然后單擊“確定”關(guān)閉對話框。具有字段名稱的節(jié)點將出現(xiàn)在數(shù)據(jù)集名稱下方。
5、從Visual Studio工具箱的ActiveReports 14 Page Report選項卡中,將Table數(shù)據(jù)區(qū)域拖到報表的設(shè)計圖面上。
6、在ReportExplorer中,將新添加的字段添加到表的詳細信息行中的單元格上。
7、將報告另存為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è)置報告并為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事件從對象加載數(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īng)用程序。
相關(guān)內(nèi)容推薦:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: