彩票走势图

DevExpress使用教程:XtraGridControl动态添加右键菜单

转帖|使用教程|编辑:我只采一朵|2014-11-04 14:17:34.000|阅读 8838 次

概述:本文介绍如何在DevExpress XtraGridControl 中动态添加右键菜单。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:


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

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

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

GridViewMenuHelper.CreateCopyCellItem(gdvw);

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

效果图如下:

DevExpress XtraGridControl

实现代码如下:

#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下载://pan.baidu.com/s/1bnCijtP

Via GarsonZhang

慧都学院2017全新DevExpress线下研修班火热报名中!



标签:DevExpressgridcontrol

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
相关产品
DevExpress Universal Subscription

行业领先的界面控件开发包,帮助企业构建卓越应用!

DevExpress WPF Subscription

高效MVVM开发模式,WPF界面解决方案首选工具,帮助企业实现酷炫动效界面。

DevExpress Silverlight Controls

高性价比高实用性的Silverlight用户界面控件套包,拥有大量的示例和帮助文档,开发者能够快速上手!

DevExpress WinForms Subscription

为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。

DevExpress DXperience Subscription

高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!

title
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP