Hier ist was ich mache. Angenommen, Ihre Datagridview (DGV) ist dataGridView1
und Context ist cms
Erste MultiSelect
& SelectionMode
Eigenschaft des DGv
zu true
& FullRowSelect
jeweils eingestellt.
//capturing current mouse coordinate to diplay menu where the mouse pointer is.
//MouseX & MouseY is an int variable and it's global to this class.
private void dataGridView1_MouseDown(object sender, MouseEventArgs e){
MouseX = e.X;
MouseY = e.Y;
}
//Avoid row selection when Right Click. This is optional you can ignore this one if you want to select the Row on Right Click
private void dataGridView1_MouseUp(object sender, MouseEventArgs e){
if (e.Button == MouseButtons.Right){
DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X, e.Y);
dataGridView1.Rows[hti.RowIndex].Selected = false;
}
}
//Time to show the menu.
private void dataGridView2_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e){
if(e.Button == MouseButtons.Right && e.RowIndex > -1){
contextMenuStrip1.Items.Clear(); //Since I am adding the menu items dynamically. I am clearing the previous menu to avoid repeatition. Ignore this step if your menu is static/predefined
foreach(DataGridViewRow gr in dataGridView1.Rows){
if(gr.Selected){
//if row is selected, I adding the Value of the First Cell as Menu Item
cms.Items.Add(gr.Cells[0].Value.ToString());
cms.Items.Add("-"); //adding menu separator
}
}
cms.Show(dataGridView1, new System.Drawing.Point(MouseX, MouseY)); //this will add First Cell Value of all selected Rows as menu item and display the Context Menu
}
}
Ich hoffe, dass dies Ihre Anforderung erfüllt.
Ja, es ist möglich. Fragen Sie mich nicht, wie bis und wenn Sie zeigen, was Sie bisher getan haben – jonju
Ich habe ein contexmenu mit Menüelementen und zugeordneten Kontextmenu-ID mit Steuerelement erstellt. Ich habe keine Ahnung, wie Sie dies für den Bereich der Elemente anwenden. – sam
plz hilf mir in dieser Hinsicht. – sam