Back to Devexpress

SpreadsheetDataSourceLoadingOptions Class

officefileapi-devexpress-dot-xtraspreadsheet-23090cf0.md

latest10.9 KB
Original Source

SpreadsheetDataSourceLoadingOptions Class

Contains options which affect how the mail merge data source is created and its data is loaded.

Namespace : DevExpress.XtraSpreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
[TypeConverter("DevExpress.XtraSpreadsheet.Design.DataSourceLoadingTypeConverter, DevExpress.XtraSpreadsheet.v25.2.Design")]
public class SpreadsheetDataSourceLoadingOptions :
    SpreadsheetNotificationOptions
vb
<TypeConverter("DevExpress.XtraSpreadsheet.Design.DataSourceLoadingTypeConverter, DevExpress.XtraSpreadsheet.v25.2.Design")>
Public Class SpreadsheetDataSourceLoadingOptions
    Inherits SpreadsheetNotificationOptions

The following members return SpreadsheetDataSourceLoadingOptions objects:

Remarks

The SpreadsheetDataSourceLoadingOptions object is accessible using SpreadsheetControl.Options.DataSourceLoading notation.

During mail merge, if the data source requires loading a custom assembly containing the Entity Framework data model, the SpreadsheetDataSourceLoadingOptions.CustomAssemblyBehavior property allows you to specify whether to load custom data assemblies, not to load them or perform additional steps to decide for each file.

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 decide whether this application allows you to load a custom data assembly.
            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 decide whether this application allows you to load a custom data assembly.
            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

Implements

INotifyPropertyChanged

Inheritance

Object ViewStatePersisterCore BaseOptions DevExpress.XtraSpreadsheet.SpreadsheetNotificationOptions SpreadsheetDataSourceLoadingOptions

See Also

SpreadsheetDataSourceLoadingOptions Members

DevExpress.XtraSpreadsheet Namespace