DEV中右键菜单如何只在非空单元格上显示?

问题:

1. 开发时,我的winform程序中有很多gridview,我希望右键菜单只在我点击非空的行时才显示,点击其他空白区域时不显示;

2. 有一个树状导航图,treelist 中的节点都有右键菜单,我希望只在我点击这个节点时才显示右键菜单,点击treelist的空白位置不显示右键菜单。

实现:

1. 

      #region 右键菜单
        private void gvSlurry_MouseUp(object sender, MouseEventArgs e)
        {
            GridHitInfo _gridHI = gvSlurry.CalcHitInfo(new Point(e.X, e.Y));
            if (e.Button == MouseButtons.Right && _gridHI.RowHandle > 0)//根据当前选中的行数非空来确定右键菜单显示。
            {
                menuRow.Show(MousePosition);
            }
        }
        #endregion

        /// <summary>
        /// 右键菜单选项弹出条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvSlurry_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            int _select = gvSlurry.SelectedRowsCount;
            menuUpdate.Enabled = false;
            menuDelete.Enabled = false;
            if (_select == 1)
            {
                menuUpdate.Enabled = true;
                menuDelete.Enabled = true;
            }
            else if (_select > 1)
            {
                menuDelete.Enabled = true;
            }
        }

需要注意的是:

这里的右键菜单使用的是ContextMenuStrip控件;

GridControl下的ContextMenuStrip不绑定控件ContextMenuStrip1;

这里用到了GridView的两个事件,一个是MouseUp事件,一个是PopupMenuShowing事件。第二个事件是用来在菜单显示之前对菜单的现实条件做一些限制,比如说我这里的选中一条记录是右键删除和更新都可用,选中多条记录时右键只有删除可用。

2. 

        private void treeList1_MouseUp(object sender, MouseEventArgs e)
        {
            TreeList _tree = sender as TreeList;
            if (Equals(e.Button, MouseButtons.Right) &&
                Equals(ModifierKeys, Keys.None) &&
                Equals(treeList1.State, TreeListState.Regular))
            {
                Point _point = new Point(Cursor.Position.X, Cursor.Position.Y);
                TreeListHitInfo _hitInfo = _tree.CalcHitInfo(e.Location);
                if (_hitInfo.HitInfoType == HitInfoType.Cell)
                {
                    _tree.SetFocusedNode(_hitInfo.Node);
                }
                else
                {
                    return;
                }

                if (_tree.FocusedNode.HasChildren)
                {
                    popupMenu1.ShowPopup(_point);
                }
                else
                {
                    popupMenu2.ShowPopup(_point);
                }
            }
        }
原文地址:https://www.cnblogs.com/Alex1994/p/9882848.html