New to Telerik UI for WinForms? Download free 30-day trial

Context Menu

The context menu used by RadPivotGrid can be easily extended. It is a common requirement to add new items to the menu or modify the existing ones.

The PivotGridContextMenuBase.Context property provides information about the element triggering the Click event. The example below will evaluate the context so that we add an additional element whenever the menu opens after click on any of the pivot data cells.

Figure 1: Custom Context Menu Item

WinForms RadPivotGrid Custom Context Menu Item

Initialize and Set Custom Menu

public PivotGridConextMenuForm()
{
    InitializeComponent();
    MyPivotGridContextMenu menu = new MyPivotGridContextMenu(this.radPivotGrid1);
    this.radPivotGrid1.PivotGridElement.ContextMenu = menu;
}

Custom Context Menu Class

public class MyPivotGridContextMenu : PivotGridContextMenu
{
    public MyPivotGridContextMenu(RadPivotGrid pivotGrid) : base(pivotGrid.PivotGridElement)
    {
    }

    protected override void AdjustItemsForContext()
    {
        base.AdjustItemsForContext();

        if (this.Context is PivotCellElement)
        {
            RadMenuItem customMenuItem = new RadMenuItem();
            customMenuItem.Text = "Export to Excel";
            RadMenuSeparatorItem separator = new RadMenuSeparatorItem();
            this.Items.Add(separator);
            customMenuItem.Click += customMenuItem_Click;
            this.Items.Add(customMenuItem);
        }
    }

    private void customMenuItem_Click(object sender, EventArgs e)
    {
        RadMenuItem item = sender as RadMenuItem;

        if (this.Context is PivotCellElement)
        {
            PivotCellElement pivotCell = this.Context as PivotCellElement;
            RadPivotGrid pivot = pivotCell.ElementTree.Control as RadPivotGrid;
            RadMessageBox.Show("Export to Excel");
        }
    }
}
In this article