c# - Datagridview context menu always shows -1 in hittest -
i trying create context menu datagridview. had tried few samples here not able understand why below returns -1 row clicked. winforms , grid populated datatable. doing wrong here?
datagridview.hittestinfo hit = dgvresults.hittest(e.x, e.y);
my code:
private void dgvresults_cellmouseclick(object sender, datagridviewcellmouseeventargs e) { if (e.columnindex >= 0 && e.rowindex >= 0 && e.button == mousebuttons.right) { datagridview.hittestinfo hit = dgvresults.hittest(e.x, e.y); \\ shows -1 if (hit.type == datagridviewhittesttype.cell) { dgvresults.currentcell = dgvresults[hit.columnindex, hit.rowindex]; cmsresults.show(dgvresults, e.x, e.y); } } }
when use mouseclick event seem work, bit lost here
private void dgvresults_mouseclick(object sender, mouseeventargs e) { if (e.button == mousebuttons.right) { int currentmouseoverrow = dgvresults.hittest(e.x, e.y).rowindex; cmsresults.show(dgvresults, new point(e.x, e.y)); } }
edit:
i got work below code.
thanks everyone
code worked me:
private void dgvresults_mouseclick(object sender, mouseeventargs e) { if (e.button == mousebuttons.right) { int currentmouseoverrow = dgvresults.hittest(e.x, e.y).rowindex; dgvresults.clearselection(); if (currentmouseoverrow >= 0) // show context menu strip if not negative { dgvresults.rows[currentmouseoverrow].selected = true; cmsresults.show(dgvresults, new point(e.x, e.y)); row = currentmouseoverrow; } } }
this normal behaviour x , y coordinates returned eventargs relative top left corner of hosting control:
mouseeventargs
return x/y coordinates relativedatagridview
control.datagridviewcellmouseeventargs
return x/y coordinates relativedatagridviewcell
control.
hittest
performed against datagridview
control , converts provided x/y column/row indexes without modifications.
illustration below demonstrates idea (with blue - values returned mouseeventargs
, green - datagridviewcellmouseeventargs)
Comments
Post a Comment