Jan P.
Jan P.

Reputation: 3297

DataGridView - Delete items with ContextMenu

I am currently developing an DataGridView to store PDF-Files via Drag and Drop. The user is able to d'n'd the file into the DataGridView and the PDF is stored to an container.

Then I wanted to realize a ContextMenu which allows the user to open or delete the PDF by right clicking on it and selecting the targeted option.

The option "Open" is working fine by using the HitTest(x, y) with the CursorPosition.

My problem is, that as you see the "Delete" button is placed in the cell bellow and the HitTest(x, y) will deliver me the cell right below, which is not my goal.

What I have tried

I've tried it by catching the CellContentClick and Click events, but those are not triggered by right clicking the cell. Also the option by saving the last entered cell by catching the CellMouseEnter event is not working propably

Is there any posibility to get to know which cell was right clicked?

Upvotes: 0

Views: 636

Answers (1)

CR41G14
CR41G14

Reputation: 5594

In Your dataGridView enable the Single Cell Selection property and then on click of your context menu you can get the selected cell

You can get the selected cell by

private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        dataGridView.CurrentCell = dataGridView[e.ColumnIndex, e.RowIndex];
    }
}

Upvotes: 1

Related Questions