1. Add ContextMenuStrip control default naming: ContextMenuStrip1
2. On the control that you want to display, locate its ContextMenuStrip property and set it to CONTEXTMENUSTRIP1
For example, with my side on the DataGridView1 control, set the DataGridView1 ContextMenuStrip property to ContextMenuStrip1
3. Write the following code to the DataGridView1 Cellmousedown event
//When you right-click, select the current row Private voidDatagridview1_cellmousedown (Objectsender, DataGridViewCellMouseEventArgs e) { if(E.button = = System.Windows.Forms.MouseButtons.Right && e.rowindex >=0) { if(!Datagridview1.rows[e.rowindex]. Selected) {datagridview1.clearselection (); Datagridview1.rows[e.rowindex]. Selected=true; } contextmenustrip1.show (mouseposition.x, MOUSEPOSITION.Y); } }
4. Add a click event for the ToolStripMenuItem control
Private void Delete Toolstripmenuitem_click (object sender, EventArgs e) { // Gets the index of the currently selected row t22 int selectrow = dataGridView1.CurrentRow.Index; if 0 ) return; }
C # Right-click menu ContextMenuStrip