• <menu id="w2i4a"></menu>
  • logo DevExpress使用教程

    文檔首頁(yè)>>DevExpress使用教程>>DevExpress使用教程:XtraGridControl動(dòng)態(tài)添加右鍵菜單

    DevExpress使用教程:XtraGridControl動(dòng)態(tài)添加右鍵菜單



    在使用 GridControl 的時(shí)候經(jīng)常需要添加右鍵菜單。一般的做法是自己創(chuàng)建菜單項(xiàng),然后注冊(cè)GridView的Mouse-Click事件,然后Show出定義好的菜單。但是涉及到一些單擊事件會(huì)收到編輯器編輯狀態(tài)影響,所以Mouse-Click事件不好用。

    幸好,GridView自帶了一個(gè)默認(rèn)的右鍵事件,專門用于彈出右鍵菜單用:PopupMenuShowing

    為了調(diào)用方便,設(shè)計(jì)成一個(gè)靜態(tài)方法,調(diào)用的時(shí)候只用傳入需要注冊(cè)的網(wǎng)格就好:

    GridViewMenuHelper.CreateCopyCellItem(gdvw);

    這里,為傳入網(wǎng)格添加一個(gè)名為【復(fù)制XXX】(XXX為列頭)的方法,可以將鼠標(biāo)點(diǎn)中的Cell中的數(shù)據(jù)復(fù)制到剪貼板。

    效果圖如下:

    DevExpress XtraGridControl

    實(shí)現(xiàn)代碼如下:

    #region 添加復(fù)制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("復(fù)制" + 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

    補(bǔ)充:

    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 添加復(fù)制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

    慧都學(xué)院2017全新DevExpress線下研修班火熱報(bào)名中!


    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();