Pick Line Index clicked with the right button, remove the option to select in datagridview

Asked

Viewed 111 times

0

First of all thank you for your time.

I have a question:

I have a Datagridview dgvPessoa filled with a Datatable.

Example:

dgvPessoa

I want you to select when right-click and not left-click, and pick the index of the line clicked.

Method to change the color of the clicked line:

private void PintarFundoDGV(DataGridView dgv, linhaClicada)
{
    if (dgv.CurrentRow.DefaultCellStyle.BackColor == Color.Transparent)
    {
        dgv.CurrentRow.DefaultCellStyle.BackColor = Color.Red;
        dgv.Rows[linhaClicada].DefaultCellStyle.BackColor = 
        Color.Transparent;
    }
}

It would change the color in the mouse click event with the right button. Catch the id and call the method

private void dgvLocacao_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button != MouseButtons.Right)
    {
        return;
    }
    else
    {
        if (dgvLocacao.CurrentCell == null)
        {
            return;
        }
        else
        {
            id = dgvLocacao.Rows[dgvLocacao.CurrentRow.Index].Cells[ID_LOCACAO.Name].Value.toInt32();
            PintarFundoDGV(dgvMeuDataGridBiew, id);
        }
    }
}
  • Hi @Gustavo. Post the code you are using, it will be easier to help.

  • So I couldn’t remove the part of taking the selection with the left. I don’t want to select, just click on the line with the right button that will open a Contexmenustrip and the line will turn red, to paint the background I use this method: > private void Pintarfundodgv(Datagridview dgv) { if (dgv.CurrentRow.Defaultcellstyle.Backcolor == Color.Transparent) { dgv.CurrentRow.Defaultcellstyle.Backcolor = Color.Red; } }

  • Edit your question and put there the structured code. In comment it is difficult to notice the sequence.

  • I hope you can understand I think you’re still confused I think what I want is simple, but I have no idea how to do it

1 answer

0

You must set:
dgvLocation.Fullrowselect = true;

//No evento que voce subscreveu irá usar algo assim: if (SuaDataGridView.SelectedRows.Count > 0) { Int32 id = dgvLocacao.Rows[dgvLocacao.SelectedRows[0].Cells[ID_LOCACAO.Name].Value.toInt32(); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.