C# - DatagridView and ContextMenuStrip -


i have datagridview 5 columns , context menu strip have items , sub items. when right click on last column want open context menu.

i tried code, it's open context menu strip without sub items.

datagrid.columns[datagrid.columns.count].headercell.contextmenustrip = contextmenustrip1; 

it looks want open contextmenustrip if user right clicks header of datagridview's last column. use datagridview mousedown event , in event check these conditions , if they're met call show method of contextmenustrip.

like this:

private void datagridview1_mousedown(object sender, mouseeventargs e) {     if (e.button == mousebuttons.right) {         var ht = datagridview1.hittest(e.x, e.y);         // see if user right-clicked on header of last column.         if ((    ht.columnindex == datagridview1.columns.count - 1)               && (ht.type == datagridviewhittesttype.columnheader)) {             // positions menu @ mouse's location.             contextmenustrip1.show(mouseposition);         }     } } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -