New to Telerik UI for WinFormsStart a free 30-day trial

Context Menu

Updated over 6 months ago

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

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

Custom Context Menu Class

C#
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");
        }
    }
}

See Also

In this article
See Also
Not finding the help you need?
Contact Support