Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Validate the ObjectDataSource Contained in the Spreadsheet MailMerge Template

  • 2 minutes to read

Loading the mail merge templates with the ObjectDataSource data source may cause undesired behavior if the data source is contained in a compiled assembly. This example illustrates how to use a custom service that implements the IObjectDataSourceValidationService interface to validate an ObjectDataSource contained in the loaded mail merge template and prevent the data source from loading.

public class MyObjectDataSourceValidationService : IObjectDataSourceValidationService {
    public void Validate(IEnumerable<ObjectDataSource> dataSources)
    {
        // Do nothing to allow the control to load data.
        // Clear the DataSource and DataMember properties to prohibit data loading.
        foreach (ObjectDataSource ds in dataSources)
        {
            if (ds.Name != "EmployeeDS")
            {
                ds.DataSource = null;
                ds.DataMember = null;
            }
        };
    }
}
using DevExpress.Spreadsheet;
using System.Diagnostics;
static void Main()
{
    Workbook workbook = new Workbook();
    workbook.ReplaceService<IObjectDataSourceValidationService>(new MyObjectDataSourceValidationService());
    workbook.LoadDocument("EmployeesMailMergeTemplate.xlsx");
    var result = workbook.GenerateMailMergeDocuments();
    result[0].SaveDocument("result.xlsx");
    Process.Start("result.xlsx");
}