DevExpress tutorial: XtraGridControl dynamically add right-click menu

When using  GridControl  , it is often necessary to add a right-click menu. The general practice is to create the menu item yourself, then register the Mouse-Click event of the GridView, and then show the defined menu. However, some click events will be affected by the editor editing state, so the Mouse-Click event is not easy to use.

Fortunately, GridView comes with a default right-click event, which is specially used to pop up the right-click menu: PopupMenuShowing

For the convenience of calling, it is designed as a static method. When calling, just pass in the grid that needs to be registered:

GridViewMenuHelper.CreateCopyCellItem(gdvw);

Here, add a method named [Copy XXX] (XXX is the column header) for the incoming grid, which can copy the data in the Cell at the mouse point to the clipboard.

The effect diagram is as follows:

DevExpress XtraGridControl

The implementation code is as follows:

#region Add copy Cell menu
        public static void CreateCopyCellItem(GridView View)
        {
            View.PopupMenuShowing += new PopupMenuShowingEventHandler(Create_CopyCellItem);
        }

        static void Create_CopyCellItem(object sender, PopupMenuShowingEventArgs e)
        {
            if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row)
            {
                if (e.HitInfo.InRowCell)
                {
                    e.Menu.Items.Add(CreateCopyMenuItem((GridView)sender, e.HitInfo.RowHandle, e.HitInfo.Column));
                }
            }
        }

        static DXMenuItem CreateCopyMenuItem(GridView view, int rowHandle, GridColumn column)
        {
            DXMenuItem copyitem = new DXMenuItem("复制" + column.Caption,
                new EventHandler(OnCopyCellClick), null);
            copyitem.Tag = column;
            return copyitem;

        }

        static void OnCopyCellClick(object sender, EventArgs e)
        {
            GridColumn col = (GridColumn)((DXMenuItem)sender).Tag;
            string filed = col.FieldName;
            Clipboard.SetDataObject(col.View.GetRowCellDisplayText(col.View.FocusedRowHandle, col), true);
        }
        #endregion

Replenish:

public class GridViewAddPopupMenuBase
   {

       EventHandler OnClearCellClick;
       string MenuName;

       public static void CreateNewCellItem(GridView View, string cMenuName, EventHandler DoClearCellClick)
       {
           GridViewAddPopupMenuBase gb = new GridViewAddPopupMenuBase();
           gb.OnClearCellClick = DoClearCellClick;
           gb.MenuName = cMenuName;


           View.PopupMenuShowing += new PopupMenuShowingEventHandler(gb.Create_NewCellItem);
       }
       void Create_NewCellItem(object sender, PopupMenuShowingEventArgs e)
       {
           if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row)
           {
               if (((GridView)sender).OptionsBehavior.Editable == true)
               {
                   if (e.HitInfo.InRowCell && e.HitInfo.Column.OptionsColumn.AllowEdit == true)
                   {
                       e.Menu.Items.Add(CreateNewMenuItem((GridView)sender, e.HitInfo.RowHandle, e.HitInfo.Column));
                   }
               }
           }
       }

       DXMenuItem CreateNewMenuItem(GridView view, int rowHandle, GridColumn column)
       {
           DXMenuItem copyitem = new DXMenuItem(MenuName.Replace("[Caption]", column.Caption.Replace("\r\n","")),
               new EventHandler(OnClearCellClick), null);
           copyitem.Tag = column;
           return copyitem;

       }


   }
   public class GridViewCreateNewCellItem : GridViewAddPopupMenuBase
   {
       #region Add copy Cell menu
       public static void CreateClearCellItem(GridView View)
       {
           CreateNewCellItem(View, "清除[Caption]", DoClear);
       }

       private static void DoClear(object sender, EventArgs e)
       {
           GridColumn col = (GridColumn)((DXMenuItem)sender).Tag;
           col.View.SetRowCellValue(col.View.FocusedRowHandle, col, DBNull.Value);
       }


       #endregion
   }

Use: Load event to increase

GridViewCreateNewCellItem.CreateClearCellItem(gv_Wool);
GridViewCreateNewCellItem.CreateClearCellItem(gv_Ast);
GridViewCreateNewCellItem.CreateClearCellItem(gv_ZJ);
GridViewCreateNewCellItem.CreateClearCellItem(gv_Process2);
GridViewCreateNewCellItem.CreateClearCellItem(gv_SpecialProcess);

Demo download: http://pan.baidu.com/s/1bnCijtP

Via GarsonZhang


===============================================================

For more exciting announcements, please continue to pay attention to DevExpress Chinese website!  Scan and follow the DevExpress Chinese website WeChat public account to get the latest news and information in time

DevExpress Chinese Network WeChat

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325016976&siteId=291194637