How to get related data to records displayed in a DataGridView in VB

Giovanni Conte 66 Reputation points
2023-11-13T18:03:57.18+00:00

I Have a DataGridView connected to a db table. I click on each row of the grid and the Cell_Click Event shows the related data of another table in a TextBox. Clicking on the BindingNavigator MoveNext button and invoking Cell_Click, I don't see the correct data in the TextBox but see the data of the previous record (that was the current just before). Only clicking once more on the MoveNext button I can see the desired data, always relative to the previos record. One upon a time, in VB6, the event RowChange overcame the issue but now, the events Leave, RowLeave, RowEnter don't immediately make the row I'm moving to, the current row. I hope I was clear. I could skip a record to solve the issue but it's not very elegant. Can someone help me?

Developer technologies | VB
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-11-14T02:23:39.9933333+00:00

    Hi @Giovanni Conte ,

    Perhaps you can try using the DataGridView.SelectionChanged Event to implement the RowChange event in VB6.

    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
        If DataGridView1.SelectedRows.Count > 0 Then
            Dim selectedRow As DataGridViewRow = DataGridView1.SelectedRows(0)
            Dim rowIndex As Integer = selectedRow.Index
            'Take further action
        End If
    End Sub
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.