How to: Customize the Data Source Wizard

  • 5 minutes to read

This example demonstrates how to implement the IDataSourceWizardCustomizationService interface to skip the default Data Source Wizard page, which is used to select the existing connection to an SQL database and move directly to the custom wizard page that enables end-users to create a new connection to one of the following data sources: Microsoft SQL Server database, Microsoft Access 97 database, or Microsoft Access 2003 database.

TIP

There is no need to implement the IDataSourceWizardCustomizationService interface if you just wish to change the list of available data source types displayed on the "Select the data source type" wizard page. To do this, use the DataSourceWizardOptions.DataSourceTypes property of the DataSourceWizardOptions object accessible from the SnapControlOptions.DataSourceWizardOptions property.

Create Custom Pages

To customize the default Data Source Wizard pages, derive from the standard class representing the wizard page you wish to modify, and override its GetNextPageType method to define the order in which the pages will appear in the wizard.

public class CustomChooseDataSourceNamePage : ChooseDataSourceNamePage<DataSourceModel> 
{
    public CustomChooseDataSourceNamePage(IChooseDataSourceNamePageView view, IDataSourceNameCreationService dataSourceNameCreator)
        : base(view, dataSourceNameCreator){ }

    // Override the GetNextPageType method to open the modified "Select the data source type" page when an end-user clicks "Next". 
    public override Type GetNextPageType()
    {
        return typeof(CustomChooseDataSourceTypePage);
    }
}

internal class CustomChooseDataSourceTypePage : ChooseDataSourceTypePage<DataSourceModel>
{
    public CustomChooseDataSourceTypePage(IChooseDataSourceTypePageView view, IWizardRunnerContext context, IConnectionStorageService connectionStorageService, ISolutionTypesProvider solutionTypesProvider, SqlWizardOptions options) : base(view, context, connectionStorageService, solutionTypesProvider, options)
    {
    }

    public override Type GetNextPageType()
    {
        // Skip the wizard page that enables usage of the existing connection to a database and directly move to the page that allows you to create a new connection. 
        if (View.DataSourceType == DataSourceType.Xpo)
            return typeof(ConnectionPropertiesPage<DataSourceModel>);
        // If the data source type is other than "Database", display the standard wizard page corresponding to the selected type.
        return base.GetNextPageType();
    }
}

The full list of the Data Source Wizard pages is available in the DevExpress.DataAccess.Wizard.Presenters namespace.

Create a Wizard Customization Service

To add custom pages to the wizard, do the following.

  1. Create a service that implements the IDataSourceWizardCustomizationService interface.
  2. Implement the IDataSourceWizardCustomizationService.CustomizeDataSourceWizard method, which accepts one parameter - an object exposing the IWizardCustomization<DataSourceModel> interface. This object provides a set of methods required for the wizard customization.

    • The StartPage method allows you to set the first page for the Data Source Wizard.
    • The RegisterPage method is used to add a custom or modified page to the wizard.
    • The Resolve method returns wizard items by their type, so that you can change them as required.
public class DataSourceWizardCustomizationService : IDataSourceWizardCustomizationService {
    public void CustomizeDataSourceWizard(IWizardCustomization<DataSourceModel> tool) {
        tool.WizardTitle = "Custom Data Source Wizard";

        // Specify the first page for the Data Source Wizard.
        tool.StartPage = typeof(CustomChooseDataSourceNamePage);
        // Register the modified "Enter the data source name" wizard page.
        tool.RegisterPage<CustomChooseDataSourceNamePage, CustomChooseDataSourceNamePage>();
        // Register the modified "Select the data source type" wizard page.
        tool.RegisterPage<CustomChooseDataSourceTypePage, CustomChooseDataSourceTypePage>();

        // Restrict the number of available data providers to three predefined items. 
        List<ProviderLookupItem> providers = (List<ProviderLookupItem>)tool.Resolve(typeof (List<ProviderLookupItem>));
        providers.Clear();
        providers.Add(new ProviderLookupItem("MSSqlServer", "Microsoft SQL Server", new MsSqlServerStrategy()));
        providers.Add(new ProviderLookupItem("Access97", "Microsoft Access 97", new Access97Strategy()));
        providers.Add(new ProviderLookupItem("Access2007", "Microsoft Access 2007", new Access2007Strategy()));
    }
}

Register a Wizard Customization Service

To launch the customized Data Source Wizard instead of the default one, add the specified customization service to the service container by using the AddService method of SnapControl.

// Register the service for the Data Source Wizard customization.   
snapControl.AddService(typeof(IDataSourceWizardCustomizationService), new DataSourceWizardCustomizationService());