Q:

Get the value with the right click on cell c# DataGridView

I am working with the next DataGridView:

I am trying to get the value of the pink cell when I right click on the black cell, but I don't know how to do it exactly because this gives me the value of the current cell (blue cell) (Code):
private void abrirIncidencia_Click(object sender, EventArgs e)
{
    int currentRowIndex = dataGridView1.CurrentRow.Index;

    string nd = dataGridView1.CurrentCell.Value.ToString();
    System.Diagnostics.Debug.WriteLine("Cell seleccionada: " + nd + " current row es: " + currentRowIndex);
    string nOrdFab = dataGridView1.Rows[currentRowIndex].Cells[1].Value.ToString();

    SqlConnection conexion = new SqlConnection(ConfigurationManager.AppSettings["ggConnectionString"].ToString());

    String strSql = "";
    SqlCommand comando;
    SqlDataAdapter adapter;

    catch (Exception ex)
    {
        MessageBox.Show(Form.ActiveForm, "Error: " + ex.ToString());
    }
}

And this method is accessed by a ToolStripMenuItem:
        this.abrirIncidencia.Name = "abrirIncidencia";
        this.abrirIncidencia.Size = new System.Drawing.Size(227, 22);
        this.abrirIncidencia.Text = "Abrir Incidencia";
        this.abrirIncidencia.Click += new System.EventHandler(this.abrirIncidencia_Click);

How can I get the value of the pink cell when I right click over the black cell?
EDIT:
In this method I am using EventsArgs but if I try to change to MouseEventArgs tell me the next error:

No overhead corresponding to 'openClick_Incidence' matches the
  'System.EventHandler'

Thanks in advance!

A:

You should handle the DataGridView's CellClick event instead:
dataGridView1.CellClick += New DataGridViewCellEventHandler(dataGridView1_CellClick)

Make a function to handle this, with the correct signature:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
    try {
        string nd = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        System.Diagnostics.Debug.WriteLine("Cell seleccionada: " + nd + " current row es: " + e.RowIndex);
        string nOrdFab = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

        SqlConnection conexion = new SqlConnection(ConfigurationManager.AppSettings["ggConnectionString"].ToString());

        String strSql = "";
        SqlCommand comando;
        SqlDataAdapter adapter;

    } catch (Exception ex)
    {
        MessageBox.Show(Form.ActiveForm, "Error: " + ex.ToString());
    }
}

It would also be beneficial to remove the Try Catch once you have this code fully operational - or at least only catch external errors thrown from the SqlAdapter. Also, if your columns are created in the designer, then your form will have a field for each column - such as IDDataGridViewTextboxColumn - which you can access the index of like IDDataGridViewTextboxColumn.Index to simplify and clarify which column you are accessing. This would make the declaration of nOrdFab much more clear; for example:
string nOrdFab = dataGridView1.Rows[e.RowIndex].Cells[IDDataGridViewTextboxColumn.Index].Value.ToString();

EDIT: If you're trying to do the work in a ContextMenuStrip event handler, then you still need to handle the CellClick event to set the current cell's selection to the one that was right clicked, but will need to use the CurrentCell property again in the event handler for the ToolStripMenuItem's Click event.
The CellClick Handler:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
    if (e.Button == MouseButtons.Right && e.RowIndex >= 0) {
        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
    }
}

Then the context menu item handler:
private void abrirIncendia_Click(object sender, EventArgs e) {
    try {
        string nOrdFab = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();

        SqlConnection conexion = new SqlConnection(ConfigurationManager.AppSettings["ggConnectionString"].ToString());

        String strSql = "";
        SqlCommand comando;
        SqlDataAdapter adapter;

    } catch (Exception ex)
    {
        MessageBox.Show(Form.ActiveForm, "Error: " + ex.ToString());
    }
}

