Programming for Everybody: Visual Basic.net: retrieve values from database in row datagridview based on cell in Same row

Visual Basic.net: retrieve values from database in row datagridview based on cell in Same row

 Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown

        If e.KeyCode = Keys.Right Then

            For i  As Integer = 0 To DataGridView1.Rows.Count - 1

                Dim constring1 As String = "data source=.;initial catalog=users;integrated security=true"

               Dim query1 As String = " select id,name,country from table2 where id='" & DataGridView1.Rows(i).Cells(0).Value & "'"

               Dim cn1 As SqlConnection = New SqlConnection(constring1)

                Dim cmd1 As SqlCommand = New SqlCommand(query1, cn1)

                cn1.Open()

                If True Then

                    Using read1 As SqlDataReader = cmd1.ExecuteReader()

                        While read1.Read()

                            DataGridView1.Rows(i).Cells(1).Value = (read1("name"))

                            DataGridView1.Rows(i).Cells(2).Value = (read1("country"))

                        End While

                    End Using

                End If

                cn1.Close()

            Next

        End If

 End Sub




No comments:

Post a Comment