我在makeDataGridView函数中创建DataGridView,也许是datagridview rightclick - >出现上下文菜单
这是示例代码
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
*/
}
我想在'context_menu_click'函数中从datagridview打印一个.txt文件。
click
函数的发件人是DataGridView,但context_menu_click
发件人是ToolStripMenu。
那么,如何在context_menu_click
函数中从DataGridView打印.txt文件?
答案 0 :(得分:0)
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
答案 1 :(得分:0)
使用此功能在您的点击事件中获取DataGridView
((ContextMenuStrip)((ToolStripMenuItem)sender).GetCurrentParent()).SourceControl