Right-click on DataGridView to show ContextMenu - but via code

0
0

I have a DataGridView with a few rows and I only want the ContextMenu to show when the user right-clicks a cell with data in it. I’ve used the MouseClick event to do this and then cmWhatever.Show() if the cell has data. All working fine, until I start to use a tablet, the right-click doesn’t work at all. I’m loathed to use a button in the grid and I guess I could hook into the “opening” event of the ContextMenu?

How can I get the tablet to show a menu on right-click of a valid cell in a DataGridView?

  • You must to post comments
1
0

Yes the LongTap is the tablet and mobile equivalent of the right click. I think the library converts a long click (press the mouse and keep it pressed) to a long tap on desktops. You can always check the environment using Application.Browser.Device (https://wisej.com/docs/2.1/html/T_Wisej_Core_ClientBrowser.htm).

  • Neil Hoskins
    Luca – thanks for helping on this. I’ve implemented what I thought was ok for LongTap, but it’s giving the wrong row when I have the browser on a second monitor. How do I get the ColumnIndex and RowIndex of the cell that has been LongTapped please?
  • Luca (ITG)
    Tried this cm.Show(Control.MousePosition); in LongTap on two monitors but I always get the correct position. A second monitor should be irrelevant to the content of the browser.
  • You must to post comments
0
0

OK – I have worked around it using the LongTap event (not sure this is correct). I also saw recommendations to use CellMouseUp rather than MouseClick (saves checking the cell is a cell, not a header or the white-space). My code for showing the ContextMenu is:

private void grdTests_LongTap(object sender, EventArgs e)
{
Wisej.Web.DataGridView.HitTestInfo htInfo = grdTests.HitTest(grdTests.PointToClient(Cursor.Position).X, grdTests.PointToClient(Cursor.Position).Y);

int iRow = htInfo.RowIndex;
int iCol = htInfo.ColumnIndex;

if (iRow >= 0 && iCol >= 0) // has to be a cell, not a header or after the last row
{
string sTestID = grdTests[“RECORDID”, iRow].Value.ToString();

cmTest.Tag = sTestID;
cmTest.Show(grdTests, grdTests.PointToClient(Cursor.Position), null);
}

}

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.