Windows Develop Bookmark and Share   
 index > Windows Forms General > add to context menu in DataGridViewTextBoxColumn
 

add to context menu in DataGridViewTextBoxColumn

I am using an unbound DataGridView control and within it have some columns based on DataGridViewTextBoxColumn. In the properties of the DataGridViewTextBoxColumn I have set the ContextMenuStrip property to point to a contextmenustrip I have manually created.

However this only works when right clicking the cell, if you are currently typing text in the cell I just get a context menu like this:

Undo
_____
Cut
Copy
Paste
etc...

Ideally I just want to be able to add an item "INSERT"to this default context menu, then sub items under it

i.e.

Undo
____
Cut
Copy
Paste
INSERT -> Insert This
Inser That
etc

I have been trying to find samples on the internet of how to do this, but all see to refer just to textbox control, and don't work with datagridviewtextboxcolumn.

Malcolm McCaffery  Wednesday, September 16, 2009 4:51 AM

Hi Malcolm,

> if you are currently typing text in the cell I just get a context menu like this:

In this case, the current cell is in edit mode. Right-clicking in the hosted editing control, i.e. a TextBox in this case, gets the built-in context menu of a TextBox to pop up.

You can replace the built-in context menu of a TextBox with your own contextmenustrip by setting the ContextMenuStrip property of the TextBox. In your scenario, you can handle the EditingControlShowing event of the DataGridView to get the hosted TextBox and then set its ContextMenuStrip property. For example:

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if(txtbox!=null)
txtbox.ContextMenuStrip = this.contextMenuStrip1;

}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Linda Liu  Wednesday, September 16, 2009 10:52 AM

Hi Malcolm,

> if you are currently typing text in the cell I just get a context menu like this:

In this case, the current cell is in edit mode. Right-clicking in the hosted editing control, i.e. a TextBox in this case, gets the built-in context menu of a TextBox to pop up.

You can replace the built-in context menu of a TextBox with your own contextmenustrip by setting the ContextMenuStrip property of the TextBox. In your scenario, you can handle the EditingControlShowing event of the DataGridView to get the hosted TextBox and then set its ContextMenuStrip property. For example:

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if(txtbox!=null)
txtbox.ContextMenuStrip = this.contextMenuStrip1;

}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Linda Liu  Wednesday, September 16, 2009 10:52 AM
thanks for the prompt response, works great.
Malcolm McCaffery  Wednesday, September 16, 2009 12:02 PM

You can use google to search for other answers

Custom Search

More Threads

• input data from textboxes
• populate the datagrid on background thread.
• Creating a windows service
• what control should i use?
• Using Window Messages
• Draw line in a user control...
• Network Printer Selection
• How to stretch a line?
• question about updating GUI components from a seperate thread.
• bindingsource currentchanged event