Home > Net >  How to open form when a specific cell in DataGridView Single-click in vb.net?
How to open form when a specific cell in DataGridView Single-click in vb.net?

Time:10-03

 ------- ------- ------- ------- 
|   a   |   b   |   c   |   d   |
 ------- ------- ------- ------- 
|   1.  |   b1  |   c1  |   d1  |
 ------- ------- ------- ------- 

The problem is how to make a cell ex: b1 click and a form will show? but when c1 or d1 clicked the form won't open, the only cursor on the place c1 or d1.

I already use CellClick event but all of the cells when I click will open the form, that's not what I want. I use VB.NET.

CodePudding user response:

Just use the _CellMouseUp event like:

Private Sub DataGridView1_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
    Dim i As Integer
    Dim j As Integer
    i = DataGridView1.CurrentCell.RowIndex
    j = DataGridView1.CurrentCell.ColumnIndex
    If i = 5 And j = 2 Then
        MsgBox(i & "  " & j)
    End If
End Sub

CodePudding user response:

Use the DataGridViewCellEventArgs. They are passed to the click event as e. The index of the first row of data (not including the headings) is 0. The colums are also indexed starting with 0. So, in your diagram column c would be index 2. You will find that .net indexes start at 0.

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If e.RowIndex = 0 AndAlso e.ColumnIndex = 2 Then
        Form2.Show()
    End If
End Sub
  • Related