Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select row in DataGridView in mouse_click event

I have a DataGridView on a form. When I right-click a row, I need the program to open a context menu. With this context menu I want to be able to modify the data in the DataGridView.

I have gotten the context menu to show where I right click, but I don't know where to go from here. As I will be deleting (for example) an entire row, I need to get the index of said row and also set it to selected. I tried this with the cell_clicked event but I can't determine if the left or right mouse button was pressed. But with the mouse_click event I cannot get the row index.

Here is my code:

public Form()
{
   ContextMenu contextMenu = new ContextMenu();

   //Fill Context Menu
   MenuItem delete = new MenuItem("Delete");
   contextMenu.MenuItems.Add(delete);
}

private void grdSchedules_MouseClick(object sender, MouseEventArgs e)
{
   if (e.Button == System.Windows.Forms.MouseButtons.Right)
   {
      contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));      
      //Get rowindex here and select row          
   }
}

I have tried it this way:

 private void grdSchedules_CellClick(object sender, DataGridViewCellEventArgs e)
    {
       if (e.Button == System.Windows.Forms.MouseButtons.Right) //e.Button does not work here
       {
           contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));                
       }
    }        
like image 959
LeonidasFett Avatar asked Jun 19 '13 17:06

LeonidasFett


2 Answers

I created a more simple and faster generic method which works with any datagrids. This method allows selecting rows with a right click. Add this method to your DataGridViews' "MouseDown" event:

    public void DataGridView_RightMouseDown_Select(object sender, MouseEventArgs e)
    {
        // If the user pressed something else than mouse right click, return
        if (e.Button != System.Windows.Forms.MouseButtons.Right) { return; }

        DataGridView dgv = (DataGridView)sender;

        // Use HitTest to resolve the row under the cursor
        int rowIndex = dgv.HitTest(e.X, e.Y).RowIndex;

        // If there was no DataGridViewRow under the cursor, return
        if (rowIndex == -1) { return; }

        // Clear all other selections before making a new selection
        dgv.ClearSelection();

        // Select the found DataGridViewRow
        dgv.Rows[rowIndex].Selected = true;
    }
like image 179
W0lfw00ds Avatar answered Sep 23 '22 04:09

W0lfw00ds


I have found a solution. Here is how I did it:

        private void grdSchedules_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                int currentMouseOverRow = grdSchedules.HitTest(e.X, e.Y).RowIndex;

                for (int x = 0; x < grdSchedules.Rows.Count; x++)
                {
                    if (grdSchedules.Rows[x].Index == currentMouseOverRow)
                    {
                        grdSchedules.Rows[x].Selected = true;
                    }
                    else
                    {
                        grdSchedules.Rows[x].Selected = false;
                    }                    
                }

                contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));

            }
        }
like image 22
LeonidasFett Avatar answered Sep 24 '22 04:09

LeonidasFett