DevExpress使用教程:XtraGridControl动态添加右键菜单
在使用 GridControl 的时候经常需要添加右键菜单。一般的做法是自己创建菜单项,然后注册GridView的Mouse-Click事件,然后Show出定义好的菜单。但是涉及到一些单击事件会收到编辑器编辑状态影响,所以Mouse-Click事件不好用。
幸好,GridView自带了一个默认的右键事件,专门用于弹出右键菜单用:PopupMenuShowing
为了调用方便,设计成一个静态方法,调用的时候只用传入需要注册的网格就好:
GridViewMenuHelper.CreateCopyCellItem(gdvw);
这里,为传入网格添加一个名为【复制XXX】(XXX为列头)的方法,可以将鼠标点中的Cell中的数据复制到剪贴板。
效果图如下:
实现代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #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 |
补充:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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事件增加
1 2 3 4 5 | 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
===============================================================
更多精彩预告请持续关注DevExpress中文网! 扫描关注DevExpress中文网微信公众号,及时获取最新动态及最新资讯

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!