翻譯|使用教程|編輯:楊鵬連|2021-02-20 10:24:42.417|閱讀 328 次
概述:該ReportEditor控件可用于創(chuàng)建、打開、保存、預(yù)覽和打印MindFusion.Reporting報表。它可以集成在任何WinForms應(yīng)用程序中,并為其提供編輯報表的能力。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
MindFusion.Reporting for WinForms是一個原生的Windows Forms編程組件,它為任何.NET應(yīng)用程序提供專業(yè)的報表功能。該組件完全使用C#語言編寫,易于使用和集成。它提供您創(chuàng)建一個完美報表所需要的一切。MindFusion.Reporting for WinForms現(xiàn)已加入在線訂購,Standard Single Developer版本原價2848現(xiàn)在搶購立享優(yōu)惠只需2345,立即查看詳情>>
點(diǎn)擊下載MindFusion.Reporting for WinForms最新試用版
該ReportEditor控件可用于創(chuàng)建、打開、保存、預(yù)覽和打印MindFusion.Reporting報表。它可以集成在任何WinForms應(yīng)用程序中,并為其提供編輯報表的能力。這對于全規(guī)模的報表應(yīng)用程序以及較小的應(yīng)用程序來說是非常有用的,它可以讓最終用戶在可視化的環(huán)境中修改現(xiàn)有的報表。例如,托管ReportEditor控件的應(yīng)用程序可用于允許最終用戶根據(jù)其需求調(diào)整預(yù)先存在的報表模板。
注釋說明
ReportEditor類位于MindFusion.Reporting.Editor.dll集合中。
注意事項
使用報表編輯器的應(yīng)用程序的最低要求 "目標(biāo)框架 "是".NET框架4"。
編輯器組件
該編輯器由四個主要組件組成--設(shè)計器區(qū)域、屬性網(wǎng)格、工具箱和預(yù)覽面板。下圖展示了ReportEditor控件的默認(rèn)外觀。
使用編輯器
用戶體驗
由于ReportEditor控件是基于在Visual Studio中實(shí)現(xiàn)MindFusion.Reporting報表設(shè)計時支持的相同組件和服務(wù),因此從用戶的角度來看,使用ReportEditor控件創(chuàng)建報表和在Visual Studio中創(chuàng)建報表幾乎是相同的。要了解如何交互式設(shè)計報表的基礎(chǔ)知識,請查看報表設(shè)計器主題。創(chuàng)建、打開和保存報表
編輯器內(nèi)顯示的報表實(shí)例可以通過報表屬性進(jìn)行訪問。注意,這個屬性是只讀的。正因為如此,要打開一個現(xiàn)有的報表進(jìn)行編輯,首先要使用它的SaveToXml方法保存報表,然后通過OpenReport方法在編輯器里面打開報表。直接通過調(diào)用Report本身的LoadFromXml打開報表是不可取的。更為一般的是,以任何方式對編輯后的報表進(jìn)行程序修改都需要謹(jǐn)慎。請查看本主題末尾的 "以編程方式修改已編輯的報表 "部分,了解如何進(jìn)行修改的提示。
要創(chuàng)建新報表或保存當(dāng)前報表,分別使用NewReport和SaveReport方法。要檢查當(dāng)前報表是否在打開或最后保存后被修改,請使用IsReportDirty屬性。每當(dāng)通過編輯器更改、打開或保存報表時,這個屬性都會自動更新。
命令
編輯器支持各種標(biāo)準(zhǔn)菜單命令,可以通過InvokeCommand方法調(diào)用。例如,要對齊當(dāng)前在編輯器中選擇的報表項的左側(cè),使用以下代碼調(diào)用標(biāo)準(zhǔn)命令A(yù)lignLeft。
C#
reportEditor.InvokeCommand(StandardCommands.Align);
Visual Basic
reportEditor.InvokeCommand(StandardCommands.Align)另外,還可以通過MenuCommandService調(diào)用命令,該命令可以通過編輯器的GetService方法獲得。
下表列出了編輯器支持的大部分標(biāo)準(zhǔn)菜單命令。
此外,編輯器還支持MenuCommands類的自定義菜單命令。
將UI元素與命令關(guān)聯(lián)起來
報告編輯器提供了監(jiān)聽支持的命令變化的方法。當(dāng)應(yīng)用程序中的一個UI元素需要與一個特定的命令相關(guān)聯(lián)時,這很有用。當(dāng)命令未啟用時,該元素需要自動灰化(或禁用)以表明這一點(diǎn)。例如,當(dāng)Undo不可用時,應(yīng)用程序中觸發(fā)Undo命令的按鈕應(yīng)該被禁用。
要監(jiān)聽命令的變化,將一個實(shí)現(xiàn)ICommandListener接口的類的實(shí)例(以及相應(yīng)命令的ID)傳遞給RegisterCommandListener方法。當(dāng)具有指定ID的命令的狀態(tài)發(fā)生變化時,通過其UpdateState方法通知指定的監(jiān)聽器。
ICommandListener接口的實(shí)現(xiàn)取決于需要更新的用戶元素的類型。例如,讓我們檢查一個應(yīng)用程序,它提供了一個ToolStripButton組件來觸發(fā)各種命令。下面是這個應(yīng)用程序的ICommandListener接口的一個示例實(shí)現(xiàn)。
C#
private class ToolStripItemCommandListener : ICommandListener
{
public CommandListener(ToolStripItem item)
{
this.item = item;
}
public void UpdateState(bool enabled, bool visible)
{
item.Enabled = enabled;
item.Visible = visible;
}
private ToolStripItem item;
}
Visual Basic
Private Class CommandListener Implements ICommandListener Public Sub New(item As ToolStripItem) Me.item = item End Sub Public Sub UpdateState(enabled As Boolean, visible As Boolean) Implements ICommandListener.UpdateState item.Enabled = enabled item.Visible = visible End Sub Private item As ToolStripItem End Class讓我們假設(shè) undoButton 是應(yīng)用程序中現(xiàn)有的 ToolStripButton,它應(yīng)該觸發(fā) Undo 命令。那么下面的代碼將把 undoButton 與 Undo 命令關(guān)聯(lián)起來。
reportEditor.RegisterCommandListener(StandardCommands.Undo, new CommandListener(undoButton));Visual Basic
reportEditor.RegisterCommandListener(StandardCommands.Undo, New CommandListener(undoButton))現(xiàn)在,當(dāng)Undo可用時,按鈕將被啟用,當(dāng)Undo不可用時,按鈕將被禁用。
注冊數(shù)據(jù)源
數(shù)據(jù)源代表報表元素可以綁定的數(shù)據(jù)對象。任何對象都可以作為數(shù)據(jù)源,包括DataSet、業(yè)務(wù)對象的數(shù)組、自定義對象等。數(shù)據(jù)源可以通過AddDataSource重載之一添加到編輯器中。新添加的數(shù)據(jù)源會以組件的形式出現(xiàn)在設(shè)計器區(qū)域底部的小托盤窗口中,隨后可以在屬性網(wǎng)格內(nèi)選擇數(shù)據(jù)源作為DataRange和Chart報表項的數(shù)據(jù)源。在編輯器中添加數(shù)據(jù)源并不會導(dǎo)致編輯的報表變臟,也不會以任何方式改變報表,直到數(shù)據(jù)源真正被選為報表項的DataSource。創(chuàng)建新報表(通過NewReport)或關(guān)閉當(dāng)前報表(通過CloseReport)不會導(dǎo)致注冊的數(shù)據(jù)源消失。
當(dāng)一個數(shù)據(jù)源被添加到編輯器中時,它將與一個唯一的名稱相關(guān)聯(lián)。該名稱在保存報表時被序列化,并在反序列化過程中用于識別項目所綁定的數(shù)據(jù)源。數(shù)據(jù)源的名稱可以通過使用 AddDataSource overload 顯式指定,也可以通過使用 AddDataSource overload 由編輯器自動生成。這兩個重載都會返回一個IComponent對象,它是編輯器內(nèi)部代表數(shù)據(jù)源的組件。在編輯器自動生成數(shù)據(jù)源名稱的情況下,這個名稱可以通過返回對象的IComponent.Site.Name屬性獲得。
DataRange對象可以從注冊的數(shù)據(jù)源中自動創(chuàng)建。要做到這一點(diǎn),請在設(shè)計器區(qū)域內(nèi)的頁面上點(diǎn)擊右鍵,并在上下文菜單中選擇 "Create DataRange from Data Source... "項。這將顯示一個對話框,里面有所有注冊的數(shù)據(jù)源和它們的字段或?qū)傩浴S脩艨梢赃x擇要包含在生成的DataRange中的成員。
以編程方式修改報表
編輯后的報表不應(yīng)直接修改。更具體地說,不應(yīng)該通過構(gòu)造函數(shù)創(chuàng)建新的項目并添加到報表或項目容器中,也不應(yīng)該通過屬性分配刪除或修改現(xiàn)有項目。直接修改不會被撤銷引擎跟蹤,不會自動更新設(shè)計器表面和UI元素,而且往往會引起異常。例如,不建議進(jìn)行以下修改。
C#
reportEditor.Report.Pages.Add(new Page())。 reportEditor.Report.Pages[0].Items[0].Location = new Location(10, 10); reportEditor.Report.Pages.RemoveAt(0);Visual Basic
reportEditor.Report.Pages.Add(New Page()) reportEditor.Report.Pages[0].Items[0].Location = New Location(10, 10) reportEditor.Report.Pages.RemoveAt(0)創(chuàng)建項目
新項目應(yīng)該通過IDesignerHost服務(wù)來創(chuàng)建。可以通過調(diào)用GetService方法從編輯器中獲得對該服務(wù)的引用。下面的代碼說明了如何創(chuàng)建一個新的頁面。
C#
IDesignerHost designerHost = reportEditor.GetService<IDesignerHost>(); Page newPage = designerHost.CreateComponent(typeof(Page)) as Page.CreateComponent(typeof(Page));Visual Basic
Dim designerHost As IDesignerHost = reportEditor.GetService(Of IDesignerHost)() Dim newPage As Page = CType(designerHost.CreateComponent(GetType(Page)), Page)添加項目
向容器中添加新的項目應(yīng)該通過調(diào)用IComponentChangeService的OnComponentChanging和OnComponentChanged方法來包裝。這個服務(wù)的引用也可以通過GetService方法獲得。下面的代碼說明了如何將之前創(chuàng)建的頁面實(shí)例添加到報表中。
C#
IComponentChangeService componentChange = reportEditor.GetService<IComponentChangeService>(); componentChange.OnComponentChanging(reportEditor.Report, null); reportEditor.Report.Pages.Add(newPage); componentChange.OnComponentChanged(reportEditor.Report, null, null, null);Visual Basic
Dim componentChange As IComponentChangeService = reportEditor.GetService(Of IComponentChangeService)() componentChange.OnComponentChanging(reportEditor.Report, Nothing) reportEditor.Report.Pages.Add(newPage) componentChange.OnComponentChanged(reportEditor.Report, Nothing, Nothing, Nothing);修改項目
PropertyDescriptor locationProperty = TypeDescriptor.GetProperties(item)["Location"]; locationProperty.SetValue(item, new Location(0, 0));Visual Basic
Dim locationProperty As PropertyDescriptor = TypeDescriptor.GetProperties(item)("Location") locationProperty.SetValue(item, New Location(0, 0))移除項目
IDesignerHost designerHost = reportEditor.GetService<IDesignerHost>()。 IComponentChangeService componentChange = reportEditor.GetService<IComponentChangeService>(); componentChange.OnComponentChanging(reportEditor.Report, null); page removedPage = reportEditor.Report.Pages[0]; reportEditor.Report.Pages.RemoveAt(0); componentChange.OnComponentChanged(reportEditor.Report, null, null, null); designerHost.DestroyComponent(removementPage);Visual Basic
Dim designerHost As IDesignerHost = reportEditor.GetService(Of IDesignerHost)() Dim componentChange As IComponentChangeService = reportEditor.GetService(Of IComponentChangeService)() componentChange.OnComponentChanging(reportEditor.Report, Nothing) Dim removedPage as Page = reportEditor.Report.Pages(0) reportEditor.Report.Pages.RemoveAt(0) componentChange.OnComponentChanged(reportEditor.Report, Nothing, Nothing, Nothing) designerHost.DestroyComponent(removementPage)備注
使用事務(wù)
對報告的每一次修改都會在撤銷歷史記錄中產(chǎn)生一條撤銷記錄。由于各種間接的修改,如新創(chuàng)建對象的屬性初始化,有些修改甚至?xí)a(chǎn)生不止一條記錄。為了將多個修改打包成一條撤銷記錄,可以在一個事務(wù)中執(zhí)行。下面的例子是在一個事務(wù)內(nèi)部修改一個現(xiàn)有項目的位置和大小。
C#
IDesignerHost designerHost = reportEditor.GetService<IDesignerHost>(); 使用(DesignerTransaction transaction = designerHost.CreateTransaction("修改項目")) { PropertyDescriptor locationProperty = TypeDescriptor.GetProperties(item)["Location"]; locationProperty.SetValue(item, new Location(0, 0)); PropertyDescriptor sizeProperty = TypeDescriptor.GetProperties(item)["Size"]; sizeProperty.SetValue(item, new Dimension(20, 20)); transaction.Commit(); }Visual Basic
Dim designerHost As IDesignerHost = reportEditor.GetService(Of IDesignerHost)() Using transaction As DesignerTransaction = designerHost.CreateTransaction("Modifying item") Dim locationProperty As PropertyDescriptor = TypeDescriptor.GetProperties(item)("Location") locationProperty.SetValue(item, New Location(0, 0)) Dim sizeProperty As PropertyDescriptor = TypeDescriptor.GetProperties(item)("Size") sizeProperty.SetValue(item, New Dimension(20, 20)) transaction.Commit() End Using也可以嵌套交易。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: