HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Saturday, July 14, 2012

WinForms Grid - Add a New Item to the Grid's Popup Menu

In this video, you will learn how to add items to the popup menu that appears when right-clicking on the grid’s footer. We will use the gridView’s “ShowGridMenu” event to add a DevExpress MenuItem to the context menu.
1.       I’ll start with a WinForms application that has a Grid Control bound to the Orders Table of the NorthWind Sample Database.
2.       First, I need to enable the footer in order to be able to access its context menu at runtime.
3.       To do this, I run the Grid’s Designer and switch to the Feature Browser.
4.       I expand “Summary”, “Total Summary” and select “Footer”.
5.       On the right, I click on the “OptionsView.ShowFooter” link to make the grid’s footer visible.
6.       And I’m done.
7.       I close the designer and return to Visual Studio.
8.       I select the gridView and create a new handler for its “ShowGridMenu” event.
9.       This event is fired when the user right-clicks on the grid’s footer.
10.   Before adding any code to the event handler, I’m going to create a custom event handler that will be triggered when the new MenuItem is clicked.
private void MyMenuItem(object sender, System.EventArgs e) {
DevExpress.Utils.Menu.DXMenuItem Item =
                (DevExpress.Utils.Menu.DXMenuItem)sender;
      DevExpress.XtraGrid.Menu.GridViewFooterMenu menu =
                (DevExpress.XtraGrid.Menu.GridViewFooterMenu)Item.Tag;
      MessageBox.Show(menu.View.FocusedColumn.Caption);
}

11.   This will create a new messagebox, displaying the name of the selected column.
12.   Now, I’m going to add the following code to the event handler.
13.   This will add a new entry, “MyItem” in our case, and associate it with the “MyMenuItem” method I created earlier.
if(e.MenuType != DevExpress.XtraGrid.Views.Grid.GridMenuType.Summary)
     return;
DevExpress.XtraGrid.Menu.GridViewFooterMenu footerMenu =
          (DevExpress.XtraGrid.Menu.GridViewFooterMenu)e.Menu;
DevExpress.Utils.Menu.DXMenuItem menuItem = new
           DevExpress.Utils.Menu.DXMenuItem("MyItem",
           new EventHandler(MyMenuItem));
menuItem.Tag = e.Menu;
footerMenu.Items.Add(menuItem);

14.   And that’s it!
15.   I run the application to see the results.
16.   I right-click on the footer of the grid and the context menu now includes the new item.
17.   I click on it and the “MyMenuItem” event handler is invoked a creating a messagebox with the name of the currently selected column.
Thanks for watching and thank you for choosing DevExpress!

Enhanced by Zemanta

No comments:

Post a Comment