current cell is not, well, current cell

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi,

I just realised that the currentcell property (and the currentrow property) doesn't necessarily mean the clicked cell is the current cell.

I need to get the index of the cell that the user right-clicked, but now I get the "active" cell instead. How do I get the cell that is clicked?

VB.NET:
 Private Sub MyDGV_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles MyDGV.CellMouseDown
        If e.Button = MouseButtons.Right Then
            If MyDGV.CurrentCell.ColumnIndex = COL_FIRSTNAME Or MyDGV.CurrentCell.ColumnIndex = COL_LASTNAME Then
                'Adds context menus
                MyDGV.Tag = MyDGV.CurrentRow.Cells(COL_STUDID).Value
                MyDGV.CurrentCell.ContextMenuStrip = UcCmPerson2.cmPerson
            End If
        End If
    End Sub

Thanks a lot!

Pettrer (Datagridview, VB.Net)
 
CellMouseDown provides column and row index for the cell in question, check the e event parameter.
 
Hi John,

Thanks again for your help!

Pettrer

PS. Here's the code, in case anyone needs it:

VB.NET:
Private Sub MyDGV_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles MyDGV.CellMouseDown
        If e.Button = MouseButtons.Right Then
            If e.ColumnIndex = COL_FIRSTNAME Or e.ColumnIndex = COL_LASTNAME And e.RowIndex > -1 Then
                'Adds context menus
                MyDGV.Tag = MyDGV.Rows(e.RowIndex).Cells(COL_STUDID).Value
                MyDGV.Rows(e.RowIndex).Cells(COL_FIRSTNAME).ContextMenuStrip = UcCmPerson2.cmPerson
                MyDGV.Rows(e.RowIndex).Cells(COL_LASTNAME).ContextMenuStrip = UcCmPerson2.cmPerson
            End If
        End If
    End Sub
 
Back
Top