, .
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.Cell)
{
dataGridView1.CurrentCell = dataGridView1[hit.ColumnIndex, hit.RowIndex];
contextMenuStrip1.Show(dataGridView1, e.X, e.Y);
}
}
}
In the Click event handler from your menu item, check the GridView1.CurrentRow data to find out which row is currently selected. For example, if the grid is bound to a data source:
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
var item = dataGridView1.CurrentRow.DataBoundItem;
}
When testing this code, make sure that the DataGridView.ContextMenuStrip property is not set.
source
share