0

I want to ask how to pass a specific cell value from a DataGridView to a Label or TextBox when I right-click on a Cell.

There is no problem with a left-click.
My code:

Private Sub DataGridView1_CellClick (ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    Dim i As Integer
    
    With DataGridView1
        if e.RowIndex >= 0 Then
            i = .CurrentRow.Index
    
            tbID.text = .Rows(i).Cells("id").Value.ToString
            tbFirstName.Text = .Rows(i).Cells("firstname").Value.ToString
            tbLastName.Text = .Rows(i).Cells("lastname").Value.ToString
            tbAddress.Text = .Rows(i).Cells("address").Value.ToString
        End If
    End With
End Sub
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Perhaps something like this might help https://stackoverflow.com/questions/1718389/right-click-context-menu-for-datagridview. I know in C# but should be pretty easy to convert. This is using the CellMouseDown event which you can test for left and right mouse buttons – Hursey Oct 20 '22 at 21:30
  • Why are you using `CurrentRow.Index` instead of `e.RowIndex` (the index of the Row clicked) – Jimi Oct 20 '22 at 22:10

0 Answers0