Skip to main content Skip to footer

How to Customize the ComponentOne FlexPivot Toolbar

The ComponentOne Studio WinForms Edition offers a FlexPivot control that is a powerful data analysis tool. This control displays a toolbar, allowing the user to perform different operations at runtime (such as loading view, saving current view to a file, exporting data to Excel, and customizing a grid view).

Toolbar

There are instances when you'll need to customize this toolbar. Recently, we came across a customer that needed to show the default file name in the Save Dialog Box when the user clicks the save button. It's not directly possible to change the default behavior of any button on the toolbar. However, the button can be replaced with a new button that will show the required behavior.

As C1FlexPivot uses the C1ToolBar instance as its toolbar, it offers the desired flexibility. So, let's examine how to achieve this functionality.

We need to add a new CommandLink and remove the existing one to save the file.

First, we create a new CommandLink that looks same as the existing Save CommandLink in appearance. Then, we remove the older one from the toolbar.

C1Command _cmd2 = new C1Command();

public Form1()
{
     InitializeComponent();

     var commandHolder = new C1CommandHolder();
     commandHolder.Commands.Add(_cmd2);
}

private void Form1_Load(object sender, EventArgs e)
{
     //Get reference to C1FlexPivot toolbar.
     C1ToolBar toolBar = _c1FlexPivotPage.ToolBar;
     //Get reference to existing Save CommandLink.
     C1CommandLink cmdLink1 = toolBar.CommandLinks[1];

     //Create a new CommandLink and add it to toolbar at index 1.
     C1CommandLink cmdLink2 = new C1CommandLink();
     toolBar.CommandLinks.Insert(1, cmdLink2);

     //Copy appearance of Save CommandLink.
     cmdLink2.ToolTipText = cmdLink1.ToolTipText;
     _cmd2.Image = cmdLink1.Command.Image;            
     cmdLink2.Command = _cmd2;

     //Remove older Save CommandLink.
     toolBar.CommandLinks.RemoveAt(2);
}

Now, we'll need to show the Save Dialog Box when this new CommandLink has been clicked. CommandClick event of CommandHolder helps us with this, where we're also using the FileName property of the Save Dialog Box to set a default name for the saved file.

SaveFileDialog _saveDialog = new SaveFileDialog();


private void CommandHolder_CommandClick(object sender, CommandClickEventArgs e)
{
      _saveDialog.Filter = "C1FlexPivot view definition file (*.olapx)|*.olapx";
      _saveDialog.Title = "Save";
      _saveDialog.FileName = "Default";
      _saveDialog.FileOk += SaveDialog_FileOk;
      _saveDialog.RestoreDirectory = true;
      _saveDialog.ShowDialog();
}


private void SaveDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
      string name = _saveDialog.FileName;
      _c1FlexPivotPage.WriteXml(name);
}

Further, we're calling WriteXml method of C1FlexPivotPage in order to save the current view to file when the user clicks on the ‘OK’ button in the Save Dialog Box. Similarly, we can customize any button on this toolbar as needed.

If you have any questions please post below.

Happy Programming!

Meenakshi Singh

comments powered by Disqus