How to display Menu by right clicking on a DataGridView C #?

1

I explain what I need: I want, by right clicking on a cell of a DataGridView that I was displayed a menu on it (on the cell) .

The problem is: When you right-click the menu, it always appears in the upper part of DataGridView and NO in the clicked cell.

Error image.

I clicked right where the red dot is but in any case the menu appears in the upper sector of DataGridView , and so in any place that right clicks.

Time to show code: The following code is the function I have so far.

    private void lista_dias_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextMenuStrip menu = new ContextMenuStrip();
            int posiscion = lista_dias.HitTest(e.X, e.Y).RowIndex;
            // if (posiscion >= 0) {
            menu.Items.Add("agregar").Name = "AGREGAR";
            menu.Items.Add("eliminar").Name = "Eliminar";
            menu.Items.Add("detalles").Name = "DETALLES";
            //}
            menu.Show(lista_dias, new Point(e.X, e.Y));
        }
    }

This event was created automatically from the interfaces that has C #, the event is CellMouseClick .

Thank you in advance for the help.

    
asked by Shassain 21.02.2018 в 15:30
source

2 answers

4

It occurs to me that you can do it by getting the coordinate of each cell.

  

Here you can get more information about the DataGridView.GetCellDisplayRectangle method

Assuming your DataGridView is called: list_dias :

private void lista_dias_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {

       lista_dias.CurrentCell = lista_dias.Rows[e.RowIndex].Cells[e.ColumnIndex];

       ContextMenuStrip menu = new ContextMenuStrip();
       menu.Items.Add("agregar").Name = "AGREGAR";
       menu.Items.Add("eliminar").Name = "Eliminar";
       menu.Items.Add("detalles").Name = "DETALLES";


       //Obtienes las coordenadas de la celda seleccionada. 
       Rectangle coordenada = lista_dias.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);

       int anchoCelda = coordenada.Location.X; //Ancho de la localizacion de la celda
       int altoCelda = coordenada.Location.Y;  //Alto de la localizacion de la celda

       //Y para mostrar el menú lo haces de esta forma:  
       int X = anchoCelda + lista_dias.Location.X;
       int Y = altoCelda + lista_dias.Location.Y + 15;

       menu.Show(lista_dias, new Point(X, Y));
    }
}
  

This way, what you request works perfectly for you. This line: lista_dias.CurrentCell = lista_dias.Rows[e.RowIndex].Cells[e.ColumnIndex]; is to place the focus in the cell that is given the Click Derecho . If you do not want to place the focus in the cell you can use lista_dias.BeginEdit(true); instead of the other mentioned line, this will show you the menu in the cell and which cell that contained the focus before clicking, do not lose the focus.

The result obtained is as follows:

I hope it helps you ... Greetings!

    
answered by 22.02.2018 / 19:26
source
1

Take this structure, I'm doing very well.

I explain: Within the grid, you right click on your record, then a tab is displayed, where it gives you an option, in my case, "Delete Product", immediately, the selected record is deleted and in turn it is Remove from the database.

For info about the technology that I apply: Entity Framework, C # and SQL server.

   public int rowIndex { get; set; }


          private void dgvProductos_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
                {
                    if (e.Button == MouseButtons.Right)
                    {
                        this.dgvProductos.Rows[e.RowIndex].Selected = true;
                        this.rowIndex = e.RowIndex;
                        this.dgvProductos.CurrentCell = this.dgvProductos.Rows[e.RowIndex].Cells[0];
                        this.contextMenuStrip1.Show(this.dgvProductos, e.Location);
                        contextMenuStrip1.Show(Cursor.Position);
                    }
                }

                private void contextMenuStrip1_Click(object sender, EventArgs e)
                {
                    ProductosEF _bdVentas = new ProductosEF();

                    if (MessageBox.Show("¿Desea eliminar el producto?", "¡Advertencia!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        string _codigo_barra = dgvProductos.Rows[rowIndex].Cells[1].Value.ToString();
                        _bdVentas.DeleteProduct(_codigo_barra);

                    }
                    _bdVentas.SaveChanges();

                    CargarGrid();
                }

I hope in one way or another to have helped solve your problem or at least an idea.

    
answered by 23.02.2018 в 13:54