Right Click and Auto Row Select with a ContextMenuStrip

by pietman 16. May 2010 12:31

If you have a datagridview and want to be able to right-click, which should automatically selecte the row that you've clicked and fire the ContextMenuStrip, it can be done as follows:

 

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        foreach (DataGridViewRow s in dataGridView1.SelectedRows)
            s.Selected = false;
        DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
        dataGridView1.Rows[hit.RowIndex].Selected = true;
        dataGridView1.CurrentCell= dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
        contextMenuStrip1.Show(dataGridView1, new Point(e.X,e.Y) );
    }
}

Tags:

c#

Add c0mment




  Country flag
biuquote
  • Comment
  • Preview
Loading


About ...

pietman celliersPietman Celliers
Bitlink  Ltd
bitlinkit.com