ComponentOne Olap for WinForms
OLAP for WinForms Quick Start / Customizing the C1OlapPage / Persisting OLAP Views
In This Topic
    Persisting OLAP Views
    In This Topic

    We will start by adding a default view to the previous application. To do this, in your Visual Studio project, right-click the project node in the solution explorer, click the "Properties" item, then select the "Settings" tab and create a new setting of type string called "DefaultView":


    This setting will be used to persist the view across sessions, so any customizations made by the user are automatically saved when he closes the application and restored next time he runs it.

    To enable this behavior, open the "Form1" form, switch to code view, and add the following code to the application:

    private void Form1_Load(object sender, EventArgs e)
        // auto-generated:
        // This line of code loads data into the 'nWINDDataSet.Invoices' table.
        // show default view: this assumes an application
        // setting of type string called "DefaultView"
        var view = Properties.Settings.Default.DefaultView;
        if (!string.IsNullOrEmpty(view))
            c1OlapPage1.ViewDefinition = view;
            // build default view now
            var olap = c1OlapPage1.OlapEngine;
    // closing form, save current view as default for next time
    protected override void OnClosing(CancelEventArgs e)
        // save current view as new default
        Properties.Settings.Default.DefaultView = c1OlapPage1.ViewDefinition;
        // fire event as usual

    The first line should already be there when you open the form. It was automatically generated to load the data.

    The next block of code checks whether the "DefaultView" setting is already available. If it is, then it is assigned to the C1OlapPage.ViewDefinition property. This applies the entire view settings, including all fields with their respective properties, as well as all charting, grid, and reporting options.

    If the "DefaultView" setting is not available, then the code creates a view by adding fields to the RowFields, ColumnFields, and ValueFields collections. The view created shows sales (sum of extended price values) by product and by country

    The next block of code overrides the form’s OnClosing method and saves the current view by reading the C1OlapPage.ViewDefinition property and assigning it to the "DefaultView" setting, which is then saved.

    If you run the project now, you will notice that it starts with the default view created by code. If you make any changes to the view, close the application, and then re-start it, you will notice that your changes are restored.