use same ContextMenu for different datagridviews
-
Hallo I put several dgv's in a form, and I created a ContextMenuStrip to perform same common operations on dgv's "CurrentRow" record (eg. Deletion). Is there any way I could reuse the same ContextMenuStrip without duplicating, maybe passing the dgv object as an argument? I would avoid to rewrite a control that inherits and extends ContextMenuStrip. Thanks in advance
-
Hallo I put several dgv's in a form, and I created a ContextMenuStrip to perform same common operations on dgv's "CurrentRow" record (eg. Deletion). Is there any way I could reuse the same ContextMenuStrip without duplicating, maybe passing the dgv object as an argument? I would avoid to rewrite a control that inherits and extends ContextMenuStrip. Thanks in advance
-
LordZoster wrote:
maybe passing the dgv object as an argument?
It's passed by default, in the sender-parameter. That's of type object, so you'd have to cast it to a
DataGridView
.I are Troll :suss:
Thanks for replying, actually I get an exception of "casting a ToolStripItem type on DataGridView type".
private void deleteToolStripMenuItem\_Click(object sender, EventArgs e) { DataGridView dgv = (DataGridView)sender; if (dgv.CurrentRow.Index > -1) dgv.Rows.Remove(dgv.CurrentRow); }
modified on Friday, March 18, 2011 6:08 AM
-
Thanks for replying, actually I get an exception of "casting a ToolStripItem type on DataGridView type".
private void deleteToolStripMenuItem\_Click(object sender, EventArgs e) { DataGridView dgv = (DataGridView)sender; if (dgv.CurrentRow.Index > -1) dgv.Rows.Remove(dgv.CurrentRow); }
modified on Friday, March 18, 2011 6:08 AM
Solved.
private void eliminaToolStripMenuItem\_Click(object sender, EventArgs e) { ToolStripDropDownItem menu = (ToolStripDropDownItem)sender; ContextMenuStrip strip = (ContextMenuStrip)menu.Owner; Control owner = strip.SourceControl; DataGridView dgv = (DataGridView)owner; if (dgv.CurrentRow.Index > -1) dgv.Rows.Remove(dgv.CurrentRow); }
-
Thanks for replying, actually I get an exception of "casting a ToolStripItem type on DataGridView type".
private void deleteToolStripMenuItem\_Click(object sender, EventArgs e) { DataGridView dgv = (DataGridView)sender; if (dgv.CurrentRow.Index > -1) dgv.Rows.Remove(dgv.CurrentRow); }
modified on Friday, March 18, 2011 6:08 AM