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.
Initialize and Set Custom Menu
public PivotGridConextMenuForm()
{
InitializeComponent();
MyPivotGridContextMenu menu = new MyPivotGridContextMenu(this.radPivotGrid1);
this.radPivotGrid1.PivotGridElement.ContextMenu = menu;
}
Public Sub New()
InitializeComponent()
Dim menu As New MyPivotGridContextMenu(Me.radPivotGrid1)
Me.radPivotGrid1.PivotGridElement.ContextMenu = menu
End Sub
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");
}
}
}
Public Class MyPivotGridContextMenu
Inherits PivotGridContextMenu
Public Sub New(ByVal pivotGrid As RadPivotGrid)
MyBase.New(pivotGrid.PivotGridElement)
End Sub
Protected Overrides Sub AdjustItemsForContext()
MyBase.AdjustItemsForContext()
If TypeOf Me.Context Is PivotCellElement Then
Dim customMenuItem As RadMenuItem = New RadMenuItem()
customMenuItem.Text = "Export to Excel"
Dim separator As RadMenuSeparatorItem = New RadMenuSeparatorItem()
Me.Items.Add(separator)
customMenuItem.Click += AddressOf customMenuItem_Click
Me.Items.Add(customMenuItem)
End If
End Sub
Private Sub customMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim item As RadMenuItem = TryCast(sender, RadMenuItem)
If TypeOf Me.Context Is PivotCellElement Then
Dim pivotCell As PivotCellElement = TryCast(Me.Context, PivotCellElement)
Dim pivot As RadPivotGrid = TryCast(pivotCell.ElementTree.Control, RadPivotGrid)
RadMessageBox.Show("Export to Excel")
End If
End Sub
End Class