C# 如何更改';对象发送方';?

C# 如何更改';对象发送方';?,c#,C#,我在makeDataGridView函数中创建DataGridView,也许DataGridView右键单击->显示上下文菜单 这是示例代码 public void click(object sender, MouseEventArgs e) { if(e.button == MouseButtons.Right) { ContextMenuStrip menu = new ContextMenuStrip(); Too

我在makeDataGridView函数中创建DataGridView,也许DataGridView右键单击->显示上下文菜单

这是示例代码

public void click(object sender, MouseEventArgs e) {
        if(e.button == MouseButtons.Right) {
               ContextMenuStrip menu = new ContextMenuStrip();
               ToolStripItem insert = menu.Items.Add("insert");
               insert.Click += new EventHandler(context_menu_click);
        }
}

public void context_menu_click(object sender, EventArgs e) {
        / *
           Other event and
           printing .txt file from DataGridView
        */
}
我想在“上下文菜单”功能中从datagridview打印一个.txt文件。
click
函数的发送方是DataGridView,但是
context\u菜单\u click
sender是ToolStripMenu

那么,如何在
上下文菜单中单击
函数从DataGridView打印.txt文件呢

public void context_menu_click(object sender, EventArgs e)
{
    /*
        Other event and
        printing .txt file from DataGridView
    */
    var item = sender as ToolStripItem  ;
    if (item != null)
    {
        DataGridView gv = item.Tag as DataGridView;
        Console.WriteLine(gv.Name);
    }
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenuStrip menu = new ContextMenuStrip();
        ToolStripItem insert = menu.Items.Add("insert");
        insert.Tag = sender;
        insert.Click += new EventHandler(context_menu_click);
        menu.Show(this.dataGridView1.PointToScreen(new Point(e.X, e.Y)));
    }
}

您可以将当前的
DataGridView
作为
Tag
添加到
ToolStripItem
您只需创建就可以使用它在单击事件中获取DataGridView

((ContextMenuStrip)((ToolStripMenuItem)sender).GetCurrentParent()).SourceControl

您是否尝试过从
单击
方法传入
发送方
e
参数?例如,
insert.Click+=neweventhandler(上下文菜单\单击(发送者,e))为什么不直接在
上下文菜单中使用
DataGridView
单击事件