# Thursday, November 27, 2008

Change selected cell in DataGridView on right click

I wanted to add a context menu to a DataGridView, allowing the user to perform an action on a specific row. However, right clicking in a DataViewGrid does not change the selected row. This can be achieved by adding the following code to the CellMouseDown event (the name of my grid is dgvWorkflows):

private void dgvWorkflows_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
    {
        dgvWorkflows.CurrentCell = dgvWorkflows.Rows[e.RowIndex].Cells[e.ColumnIndex];
    }
}

#    Comments [0] |
Comments are closed.