DevExpress中文教程:XtraGridControl动态添加右键菜单
作者: 来源:慧都控件网 浏览:Loading...次 发布时间:2015-05-14 评论:0条
在使用 GridControl 的时候经常需要添加右键菜单。一般的做法是自己创建菜单项,然后注册GridView的Mouse-Click事件,然后Show出定义好的菜单。但是涉及到一些单击事件会收到编辑器编辑状态影响,所以Mouse-Click事件不好用。<DevExpress下载>
幸好,GridView自带了一个默认的右键事件,专门用于弹出右键菜单用:PopupMenuShowing
为了调用方便,设计成一个静态方法,调用的时候只用传入需要注册的网格就好:
GridViewMenuHelper.CreateCopyCellItem(gdvw);
这里,为传入网格添加一个名为【复制XXX】(XXX为列头)的方法,可以将鼠标点中的Cell中的数据复制到剪贴板。
效果图如下:
实现代码如下:
#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
补充:
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事件增加
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控件中文网 [http://www.devexpresscn.com/]
本文地址:http://www.devexpresscn.com/Resources/Documentation-440.html
关键字: DevExpress
评论列表
暂无评论
请谈谈你的看法 请使用IE或者Firefox浏览器,暂不支持Chrome!
慧都控件网为DevExpress界面控件的中国地区唯一正式授权经销商,正版控件销售公司,授权代理商,经销商及合作伙伴。
电话:400-700-1020
023-66090381
邮箱:sales@evget.com
相关资源
- DevExpress 2014.1.5发布
- DevExtreme教程:如何不用VS创建和部署DXTREME程序
- DevExtreme教程:如何创建数据模型
- DevExtreme教程:路由规则介绍
- DevExtreme教程:如何声明路由规则
- DevExtreme教程:路由主题详解
- DevExtreme教程:如何在应用程序中进行视图设置
- DevExtreme教程:布局设置详解
- DevExpress 2014.1.6发布
- DevExtreme教程:详解平台特定标记
- GridControl导出PDF时如何设置纸张大小
- DevExtreme教程:命令详解
- DevExtreme教程:多渠道应用
- Devexpress中LayoutControl控件动态生成控件
- DevExpress 2014.1.7发布
- DevExtreme教程:创建一个WCF OData服务
- DevExpress年终大促 最高直降4万3!还有惊喜豪礼相送!
- DevExpress v14.2重磅来袭,发布会促销活动惊喜不断!
- DevExpress 14.2发布会火热报名中,官方CTO亲临坐镇!
- DevExpress v14.2.7发布