Question

I've got a DevExpress GridControl, which has a ContextMenuStrip with 2 Items on it. I want to be able right click a record in the GridControl and launch the user's default browser and search for a term using their default search engine with one of the items in the ContextMenu.

My code:

    int rowX, rowY;

    private void genericView_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            rowX = MousePosition.X;
            rowY = MousePosition.Y;
        }
    }

    private void tsmSearch_Click(object sender, EventArgs e)
    {
        int key = GetRowAt(gdcErrorLogDefaultView, rowX, rowY);

        if (key < 0)
            return;

        string ex = gdcErrorLogDefaultView.GetRowCellValue(key, "Exception").ToString();
        //Logic to launch browser & search for ex
    }

    public int GetRowAt(GridView view, int x, int y)
    {
        return view.CalcHitInfo(new Point(x, y)).RowHandle;
    }

I know GetRowAt calculates the row properly, I use it for a number of other purposes elsewhere in my code. However, it is not properly getting a key in my tsmSearch_Click event.

While testing, I set a breakpoint on my if statement in the Click event. key = -2147483648. I expect 0 because in this particular test there's only 1 row in my grid.

Is there a different way to achieve this? The grid supports multiselect, so I don't want to "overwrite" their selection by programmatically selecting the row as soon as they right click.

Here's a screenshot of what I'm trying to describe: enter image description here

Was it helpful?

Solution

And of course as soon as I finally decide to post this question, I realized my problem. The MouseDown event should be as follows:

private void genericView_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        rowX = e.X;
        rowY = e.Y;
    }
}

OTHER TIPS

I expect 0 because in this particular test there's only 1 row in my grid

It is incorrect to expect 0 in this case. Moreover the -2147483648 is a valid value for Row Handle (this value representing the invalid row handle). Please refer to the Identifying Rows and Cards help-article for more information regarding row handles.

I believe in your case it is better to obtain the handles of selected rows by the ColumnView.GetSelectedRows method. Refer to the Selections Overview document for more information.

Related examples:

  1. How to: Identify the Grid's Element Located Under the Mouse Cursor
  2. How to: Obtain and Modify Selected Rows
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top