|
Article relates to
|
RadGridView for WinForms
|
|
Created by
|
Kiril Matev
|
|
Last modified
|
April 2, 2008
|
|
Last modified by
|
Kiril Matev
|
PROBLEM
Oftentimes, applications need to provide different context menus depending on the element of a control that was clicked. This allows to make available a great amount of functionality without burdening the user with additional screen transitions or extra controls on the screen.
HOW-TO
Use different custom context menus with
RadGridView for WinForms, depending on the element of the control clicked.
SOLUTION
RadGridView provides a vehicle to use custom context menus, instead of the default one, depending on the element clicked. To learn how to set a custom context menu to appear everytime the user right-clicks the RadGridView, regardless of the element of the control they click, please visit RadGridView Custom Context Menu KB Article.
In this example, we will create two different context menus, and attach them to cells in the second and third columns in RadGridView. If the user right-clicks in a cell the second column, they will get the first custom context menu. If they click in the third column, they will get the second context menu. If they right-click any other element of the RadGridView, the default context menu will be shown.
Start by creating the context menus, initializing its items, and subscribing for the events that you want to handle to achieve the desired behavior.
| RadDropDownMenu firstContextMenu = new RadDropDownMenu(); |
| RadDropDownMenu secondContextMenu = new RadDropDownMenu(); |
| RadMenuItem firstContextMenuItem1 = new RadMenuItem("First menu - Item 1"); |
| firstContextMenuItem1.ForeColor = Color.Red; |
| firstContextMenuItem1.Click += new EventHandler(firstContextMenuItem1_Click); |
| |
| RadMenuItem firstContextMenuItem2 = new RadMenuItem("First menu - Item 2"); |
| firstContextMenuItem2.Click += new EventHandler(firstContextMenuItem2_Click); |
| |
| firstContextMenu.Items.Add(firstContextMenuItem1); |
| firstContextMenu.Items.Add(firstContextMenuItem2); |
| |
| RadMenuItem secondContextMenuItem1 = new RadMenuItem("Second menu - Item 1"); |
| secondContextMenuItem1.ForeColor = Color.LightBlue; |
| secondContextMenuItem1.Click += new EventHandler(secondContextMenuItem1_Click); |
| |
| RadMenuItem secondContextMenuItem2 = new RadMenuItem("Second menu - Item 2"); |
| secondContextMenuItem2.Click += new EventHandler(secondContextMenuItem2_Click); |
| |
| secondContextMenu.Items.Add(secondContextMenuItem1); |
| secondContextMenu.Items.Add(secondContextMenuItem2); |
Subscribe to the ContextMenuOpening event, and in its handler, add the conditions to be used to set the corresponding context menu to be displayed upon clicking of an item.
| |
| void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e) |
| { |
| GridDataCellElement cell = e.ContextMenuProvider as GridDataCellElement; |
| if (cell == null) |
| { |
| return; |
| } |
| //set the first context menu to be displayed for cells in the second column |
| if (cell.ColumnIndex == 1) |
| { |
| e.ContextMenu = firstContextMenu; |
| } |
| //set the second context menu to be displayed for cells in the third column |
| else if (cell.ColumnIndex == 2) |
| { |
| e.ContextMenu = secondContextMenu; |
| } |
| } |
| |
This example illustrates only one of the scenarios, possible with the context menu infrastructure in the RadGridView. You can set a context menu depending on a variety of conditions, such as a particular value in a column, or a complex condition including multiple values from a row.
Please
Sign In
to rate this article.