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:
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;
}
}