Get data from a cell of a datagridview in the CurrentCellChanged event in Vb?

0

I have the following event:

Private Sub zGrid_CurrentColumnChanged(ByVal sender As Object, ByVal e As EventArgs) Handles zGrid.CurrentCellChanged
    Dim CodArt As String = zGrid.Item(0, e.RowIndex).Value.ToString()
End Sub

but I do not recognize the e.RowIndex because it is inherited from DataGridViewCellEventArgs

and if I go through parameter DataGridViewCellEventArgs instead of EventArgs, the program gives me an error

I am trying to make a calculation with the cod art of that cell when changing cell with the navigation keys

    
asked by Acd 24.04.2017 в 18:57
source

1 answer

1

A possible solution is to capture the SelectionChanged event from the DataGridView object.

The issue is that it triggers repeatedly (eg: when a cell is selected, or an entire row, or multiple cells ) so you can limit this action by modifying two properties of the DataGridView: MultiSelect and SelectionMode .

Then the code you should use is this:

Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
    If DataGridView1.SelectedCells.Count <> 0 Then
        Dim CodArt As String = DataGridView1.SelectedCells(0).Value.ToString()
    End If
End Sub

EDIT:

The code that I programmed in the SelectionChanged event is a "similar" to what you have programmed but that code is wrong if you intend that, moving through a row cell by cell, it is captured a particular column and not every cell.

To put it another way with the code present above you define the variable "CodArt" with the value of the cell you are moving to.

If your intention is to recover the "CodArt" column of the row in which you are, regardless of the cell the code to use is the following:

Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
    If DataGridView1.SelectedCells.Count <> 0 Then
        'N de Fila:
        Dim NFila As Integer = DataGridView1.SelectedCells(0).RowIndex

        'Con el N de Fila, me posiciono y recupero la Columna 'CodArt'
        Dim CodArt As String = DataGridView1.Rows(NFila).DataBoundItem("CodArt").ToString

    End If
End Sub
    
answered by 24.04.2017 / 19:35
source