0

我正在尝试通过使用右键菜单来修改 DataGridView 对象。

目前我可以成功创建右键菜单并确定选择菜单项后将调用的方法,但是目标方法无法访问有关在选择菜单项时选择了 DataGridView 中的哪条记录的信息 - - 或与此相关的任何其他信息(除非它是类级变量)。

我的目标是找到一种将信息发送到目标方法的方法,或者找到一种在创建右键单击菜单的同一方法中修改 DataGridView 对象的方法。

https://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem(v=vs.110).aspx

        private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextMenu m = new ContextMenu();

                if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
                {
                    m.MenuItems.Add(new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber), this.markInspectionPointComplete));
                }
            m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
        }
    }

    private void markInspectionPointComplete(object sender, EventArgs e)
    {
        MessageBox.Show("the right-click menu works.");
    }

我尝试使用DataGridViewCellMouseEventArgs对象调用目标方法,但这会在m.MenuItems.Add()行中产生错误,因为它只需要一个EventArgs对象。

因此,要么我需要修改发送的 EventArgs 对象,要么找到另一个用于此目的的方法签名,要么找到一种方法来在创建右键单击菜单的同一方法中对 DataGridView 执行操作。

提前致谢!

4

1 回答 1

0

最简单的解决方案是使用“MenuItem”对象的“Tag”属性。

private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenu m = new ContextMenu();

        int currentRow = e.RowIndex;
            if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
            {
                var MI = new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber), this.markInspectionPointComplete)
                MI.Tag = e;
                m.MenuItems.Add(MI);
            }
        m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
    }
}

private void markInspectionPointComplete(object sender, EventArgs e)
{
    var MI = (MenuItem)sender;
    DataGridViewCellMouseEventArgs Obj = (DataGridViewCellMouseEventArgs) MI.Tag;

    // Do whatever you want with your Obj

    MessageBox.Show("the right-click menu works.");
}

或者

像这样使用 lambda 表达式:

private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenu m = new ContextMenu();

        int currentRow = e.RowIndex;
            if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
            {
                var MI = new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber));
                MI.Click += (s, x) =>
                {
                    // Use 'e' or 'sender' here
                }
                m.MenuItems.Add(MI);
            }
        m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
    }
}
于 2017-06-02T14:48:00.663 回答