开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 3615|回复: 1

XtraGridControl动态添加右键菜单

[复制链接]

0

精华

8

贡献

1768

赞扬

特约版主

帖子
583
软币
4524
在线时间
275 小时
注册时间
2019-2-21
发表于 2019-3-28 14:44:28 | 显示全部楼层 |阅读模式

DevExpress Universal Suite最新版下载

在使用 GridControl 的时候经常需要添加右键菜单。一般的做法是自己创建菜单项,然后注册GridView的Mouse-Click事件,然后Show出定义好的菜单。但是涉及到一些单击事件会收到编辑器编辑状态影响,所以Mouse-Click事件不好用。

幸好,GridView自带了一个默认的右键事件,专门用于弹出右键菜单用:PopupMenuShowing

为了调用方便,设计成一个静态方法,调用的时候只用传入需要注册的网格就好:

GridViewMenuHelper.CreateCopyCellItem(gdvw);

这里,为传入网格添加一个名为【复制XXX】(XXX为列头)的方法,可以将鼠标点中的Cell中的数据复制到剪贴板。

实现代码如下:

[C#] 纯文本查看 复制代码
#region 添加复制Cell菜单
        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

补充:

[C#] 纯文本查看 复制代码
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 添加复制Cell菜单
       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
   }

使用:Load事件增加

[C#] 纯文本查看 复制代码
GridViewCreateNewCellItem.CreateClearCellItem(gv_Wool);
GridViewCreateNewCellItem.CreateClearCellItem(gv_Ast);
GridViewCreateNewCellItem.CreateClearCellItem(gv_ZJ);
GridViewCreateNewCellItem.CreateClearCellItem(gv_Process2);
GridViewCreateNewCellItem.CreateClearCellItem(gv_SpecialProcess);

Demo下载:http://pan.baidu.com/s/1bnCijtP

Via GarsonZhang


回复

使用道具 举报

0

精华

0

贡献

220

赞扬

帖子
73
软币
1143
在线时间
126 小时
注册时间
2019-3-30
发表于 2019-4-11 21:16:56 | 显示全部楼层
连接失效了,能否重新发一下,谢谢楼主
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-12-4 16:55

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表