Back to Devexpress

SpreadsheetControl.CustomAssemblyLoading Event

windowsforms-devexpress-dot-xtraspreadsheet-dot-spreadsheetcontrol-acf819e0.md

latest12.1 KB
Original Source

SpreadsheetControl.CustomAssemblyLoading Event

Occurs before a custom assembly is loaded for use as the Entity Framework data source during mail merge and allows you to cancel the operation.

Namespace : DevExpress.XtraSpreadsheet

Assembly : DevExpress.XtraSpreadsheet.v25.2.dll

NuGet Package : DevExpress.Win.Spreadsheet

Declaration

csharp
public event SpreadsheetCustomAssemblyLoadingEventHandler CustomAssemblyLoading
vb
Public Event CustomAssemblyLoading As SpreadsheetCustomAssemblyLoadingEventHandler

Event Data

The CustomAssemblyLoading event's data class is SpreadsheetCustomAssemblyLoadingEventArgs. The following properties provide information specific to this event:

PropertyDescription
CancelGets or sets a value indicating whether the event should be canceled. Inherited from CancelEventArgs.
ContextNameGets the name of the Entity Framework context.
HandledGets or sets whether the e.Cancel setting in the event handler makes the final decision on loading the assembly.
PathGets the path to the external assembly containing the Entity Framework model.

Remarks

The CustomAssemblyLoading event is raised if the SpreadsheetDataSourceWizardOptions.CustomAssemblyBehavior or the SpreadsheetDataSourceLoadingOptions.CustomAssemblyBehavior property is set to the SpreadsheetCustomAssemblyBehavior.Prompt value.

You can subscribe to the CustomAssemblyLoading event, analyze the SpreadsheetCustomAssemblyLoadingEventArgs.Path string and set the event arguments as follows:

AssignmentMeaning
e.Cancel = falseApprove the decision to load a custom assembly.
e.Cancel = trueRefuse to load a custom assembly.
e.Handled = trueThe decision is made.
e.Handled = falseThe decision is not yet made. The service should make the final decision.

Example

csharp
using DevExpress.DataAccess.EntityFramework;
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet.Services;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace MailMergeEFData {
    public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm {
        public Form1() {
            InitializeComponent();
            // Handle this event to load a custom data assembly based on a specific condition.
            EFDataSource.BeforeLoadCustomAssemblyGlobal += EFDataSource_BeforeLoadCustomAssemblyGlobal;
            // Prompt for loading a data assembly; default is NeverLoad.
            this.spreadsheetControl1.Options.DataSourceLoading.CustomAssemblyBehavior = DevExpress.XtraSpreadsheet.SpreadsheetCustomAssemblyBehavior.Prompt;
            // Handle this event to decide whether to load a custom data assembly.
            this.spreadsheetControl1.CustomAssemblyLoading += SpreadsheetControl1_CustomAssemblyLoading;
            // The service is employed when loading a template.
            this.spreadsheetControl1.ReplaceService<ICustomAssemblyLoadingNotificationService>(new myCustomAssemblyLoadingNotificationService());
        }

        private void EFDataSource_BeforeLoadCustomAssemblyGlobal(object sender, DevExpress.DataAccess.EntityFramework.BeforeLoadCustomAssemblyEventArgs args) {
            args.AllowLoading = true;
        }

        private void SpreadsheetControl1_CustomAssemblyLoading(object sender, SpreadsheetCustomAssemblyLoadingEventArgs e) {
            // Decide whether to load a custom assembly.
            e.Cancel = MessageBox.Show(String.Format("Do you want to load data from {0}?", e.Path),
                    "CustomAssemblyLoading Event", MessageBoxButtons.YesNo) == DialogResult.No;
            // Decide whether to query the service for the final decision.
            e.Handled = MessageBox.Show(String.Format("Query the service for the final decision?", e.Path),
                    "CustomAssemblyLoading Event", MessageBoxButtons.YesNo) == DialogResult.No; ;
        }

        private void Form1_Load(object sender, EventArgs e) {
            EFDataSource ds = new EFDataSource(new EFConnectionParameters());
            ds.Name = "Contacts";
            ds.ConnectionParameters.CustomAssemblyPath = Application.StartupPath + @"\EFDataModel.dll";
            ds.ConnectionParameters.CustomContextName = "EFDataModel.ContactsEntities";
            ds.ConnectionParameters.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Contacts.mdf;integrated security=True;";
            this.spreadsheetControl1.Document.MailMergeDataSource = ds;
            this.spreadsheetControl1.Document.MailMergeDataMember = "Customers";            
            this.spreadsheetControl1.Document.Worksheets[0].Cells["A1"].Formula = "=FIELD(\"Company\")";

            try {
                IList<IWorkbook> resultWorkbooks = spreadsheetControl1.Document.GenerateMailMergeDocuments();
                string filename = "SavedDocument0.xlsx";
                resultWorkbooks[0].SaveDocument(filename, DocumentFormat.Xlsx);
                System.Diagnostics.Process.Start(filename);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "Exception");
            }
        }

        private void tglShowWizardBrowseButton_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            // Show the button in the Data Source Wizard to launch the "Browse for assembly" dialog.
            this.spreadsheetControl1.Options.DataSourceWizard.ShowEFWizardBrowseButton = tglShowWizardBrowseButton.Checked;
        }
    }

    public class myCustomAssemblyLoadingNotificationService : ICustomAssemblyLoadingNotificationService {
        public bool RequestApproval(string assemblyPath) {
            return MessageBox.Show(String.Format("Are you sure?\nLoading {0}", assemblyPath),
                "CustomAssemblyLoadingNotificationService", MessageBoxButtons.OKCancel) == DialogResult.OK;
        }
    }
}
vb
Imports DevExpress.DataAccess.EntityFramework
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraSpreadsheet.Services
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms

Namespace MailMergeEFData
    Partial Public Class Form1
        Inherits DevExpress.XtraBars.Ribbon.RibbonForm

        Public Sub New()
            InitializeComponent()
            ' Handle this event to load a custom data assembly based on a specific condition.
            AddHandler EFDataSource.BeforeLoadCustomAssemblyGlobal, AddressOf EFDataSource_BeforeLoadCustomAssemblyGlobal
            ' Prompt for loading a data assembly; default is NeverLoad.
            Me.spreadsheetControl1.Options.DataSourceLoading.CustomAssemblyBehavior = DevExpress.XtraSpreadsheet.SpreadsheetCustomAssemblyBehavior.Prompt
            ' Handle this event to decide whether to load a custom data assembly.
            AddHandler Me.spreadsheetControl1.CustomAssemblyLoading, AddressOf SpreadsheetControl1_CustomAssemblyLoading
            ' The service is employed when loading a template.
            Me.spreadsheetControl1.ReplaceService(Of ICustomAssemblyLoadingNotificationService)(New myCustomAssemblyLoadingNotificationService())
        End Sub

        Private Sub EFDataSource_BeforeLoadCustomAssemblyGlobal(ByVal sender As Object, ByVal args As DevExpress.DataAccess.EntityFramework.BeforeLoadCustomAssemblyEventArgs)
            args.AllowLoading = True
        End Sub

        Private Sub SpreadsheetControl1_CustomAssemblyLoading(ByVal sender As Object, ByVal e As SpreadsheetCustomAssemblyLoadingEventArgs)
            ' Decide whether to load a custom assembly.
            e.Cancel = MessageBox.Show(String.Format("Do you want to load data from {0}?", e.Path), "CustomAssemblyLoading Event", MessageBoxButtons.YesNo) = DialogResult.No
            ' Decide whether to query the service for the final decision.
            e.Handled = MessageBox.Show(String.Format("Query the service for the final decision?", e.Path), "CustomAssemblyLoading Event", MessageBoxButtons.YesNo) = DialogResult.No

        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Dim ds As New EFDataSource(New EFConnectionParameters())
            ds.Name = "Contacts"
            ds.ConnectionParameters.CustomAssemblyPath = Application.StartupPath & "\EFDataModel.dll"
            ds.ConnectionParameters.CustomContextName = "EFDataModel.ContactsEntities"
            ds.ConnectionParameters.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Contacts.mdf;integrated security=True;"
            Me.spreadsheetControl1.Document.MailMergeDataSource = ds
            Me.spreadsheetControl1.Document.MailMergeDataMember = "Customers"
            Me.spreadsheetControl1.Document.Worksheets(0).Cells("A1").Formula = "=FIELD(""Company"")"

            Try
                Dim resultWorkbooks As IList(Of IWorkbook) = spreadsheetControl1.Document.GenerateMailMergeDocuments()
                Dim filename As String = "SavedDocument0.xlsx"
                resultWorkbooks(0).SaveDocument(filename, DocumentFormat.Xlsx)
                System.Diagnostics.Process.Start(filename)
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Exception")
            End Try
        End Sub

        Private Sub tglShowWizardBrowseButton_CheckedChanged(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles tglShowWizardBrowseButton.CheckedChanged
            ' Show the button in the Data Source Wizard to launch the "Browse for assembly" dialog.
            Me.spreadsheetControl1.Options.DataSourceWizard.ShowEFWizardBrowseButton = tglShowWizardBrowseButton.Checked
        End Sub
    End Class

    Public Class myCustomAssemblyLoadingNotificationService
        Implements ICustomAssemblyLoadingNotificationService

        Public Function RequestApproval(ByVal assemblyPath As String) As Boolean Implements ICustomAssemblyLoadingNotificationService.RequestApproval
            Return MessageBox.Show(String.Format("Are you sure?" & ControlChars.Lf & "Loading {0}", assemblyPath), "CustomAssemblyLoadingNotificationService", MessageBoxButtons.OKCancel) = DialogResult.OK
        End Function
    End Class
End Namespace

See Also

Mail Merge in WinForms Spreadsheet Control

CustomAssemblyBehavior

ICustomAssemblyLoadingNotificationService

SpreadsheetControl Class

SpreadsheetControl Members

DevExpress.XtraSpreadsheet Namespace