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

    In addition to the ViewDefinition property, which gets or sets the current view as an XML string, the C1OlapPage control also exposes ReadXml and WriteXml methods that allow you to persist views to files and streams. These methods are automatically invoked by the C1OlapPage when you click the "Load" and "Save" buttons in the built-in toolstrip.

    These methods allow you to implement predefined views very easily. To do this, start by creating some views and saving each one by pressing the "Save" button. For this sample, we will create five views showing sales by:

    1. Product and Country
    2. Salesperson and Country
    3. Salesperson and Year
    4. Salesperson and Month
    5. Salesperson and Weekday

    Once you have created and saved all the views, create a new XML file called "OlapViews.xml" with a single "OlapViews" node, and then copy and paste all your default views into this document. Next, add an "id" tag to each view and assign each one a unique name. This name will be shown in the user interface (it is not required by OLAP). Your XML file should look like this:

    <OlapViews>
      
      <C1OlapPage id="Product vs Country">
        <!-- view definition omitted... -->
      <C1OlapPage id="SalesPerson vs Country">
        <!-- view definition omitted... -->
      <C1OlapPage id="SalesPerson vs Year">
        <!-- view definition omitted... -->
      <C1OlapPage id="SalesPerson vs Month">>
        <!-- view definition omitted... -->
      <C1OlapPage id="SalesPerson vs Weekday">
        <!-- view definition omitted... -->
     
    </OlapViews>
    
    

    Now add this file to the project as a resource. To do this, follow these steps:

    1. Right-click the project node in the solution explorer, and click "Properties".
    2. Select the "Resources" tab and click the drop-down arrow next to "Add Resource".
    3. Select the "Add Existing File…" option, choose the XML file and click Open.

    Now that the view definitions are ready, we need to expose them in a menu so the user can select them. To do this, copy the following code into the project:

    
    private void Form1_Load(object sender, EventArgs e)
    {
        // auto-generated:
        // This line of code loads data into the 'nWINDDataSet.Invoices' table.
        this.invoicesTableAdapter.Fill(this.nwindDataSet.Invoices);
     
        // build menu with predefined views:
        var doc = new System.Xml.XmlDocument();
        doc.LoadXml(Properties.Resources.OlapViews);
        var menuView = new ToolStripDropDownButton("&View");
        foreach (System.Xml.XmlNode nd in doc.SelectNodes("OlapViews/C1OlapPage"))
        {
            var tsi = menuView.DropDownItems.Add(nd.Attributes["id"].Value);
            tsi.Tag = nd;
        }
        menuView.DropDownItemClicked += menuView_DropDownItemClicked;
        c1OlapPage1.Updated += c1OlapPage1_Updated;
     
        // add new view menu to C1OlapPage toolstrip
        c1OlapPage1.ToolStrip.Items.Insert(3, menuView);
    }
    
    

    The code creates a new toolstrip drop-down button, then loads the XML document with the report definitions and populates the drop-down button with the reports found. Each item contains the view name in its Text property, and the actual XML node in its Tag property. The node will be used later to apply the view when the user selects it.

    Once the drop-down is ready, the code adds it to the C1OlapPage using the ToolStrip property. The new button is added at position 3, after the first two buttons and the first separator.

    The only part still missing is the code that will apply the views to the C1OlapPage when the user selects them by clicking the button. This is accomplished with the following code:

    // select a predefined view
    void menuView_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        var nd = e.ClickedItem.Tag as System.Xml.XmlNode;
        if (nd != null)
        {
       // load view definition from XML
       c1OlapPage1.ViewDefinition = nd.OuterXml;
     
       // show current view name in status bar
       c1OlapPage1.LabelStatus.Text = nd.Attributes["id"].Value;
        }
    }
    void c1OlapPage1_Updated(object sender, EventArgs e)
    {
        // clear report name after user made any changes
        c1OlapPage1.LabelStatus.Text = string.Empty;
    }
    

    The code retrieves the report definition as an XML string by reading the node’s OuterXml property, then assigns it to the ViewDefinition property. It also shows the name of the view in the C1OlapPage status bar using the LabelStatus property.

    Finally, the code handles the Updated event to clear the status bar whenever the user makes any changes to the view. This indicates that the view no longer matches the predefined view that was loaded from the application resources.

    The C1OlapPage exposes most of the components it contains, which makes customization easy. You can add, remove or change the elements from the ToolStrip, from the TabControl, and show status messages using the LabelStatus property. You can also add other elements to the page in addition to the C1OlapPage.

    If you need further customization, you can also choose not to use the C1OlapPage at all, and build your interface using tee lower-level C1OlapPanelC1OlapGrid, and C1OlapChart controls. The source code for the C1OlapPage control is included with the package and can be used as a starting point. The example in the "Building a custom User Interface" section shows how this is done.