Load data from a table (Database) to already designed columns in a datagridview

ELIRDFUL 100 Reputation points

Hello everyone.

 I have a datagridview, designed with two specific columns.

Sin título

with this code I try to fill the columns, predesigned

            Using Conexion As New SqlConnection(cadenaconexion)
                Dim consultaSQL As String = "Select CodigoProducto, DescripcionProducto from TblaDetalleFactura2 WHERE CodigoFactura =@CodigoFactura"

                Dim comando As New SqlCommand(consultaSQL, Conexion)
                comando.Parameters.AddWithValue("@CodigoFactura", txtCodigoFactura.Text)
                Dim LD As SqlDataReader
                LD = comando.ExecuteReader()

                If LD.Read() Then

                    Dim CodigoProductoEncontrado As String
                    Dim DescripcionProductoEncontrado As String

                    'Establecemos valores de la asignación, en el mismo orden de los valores de la consulta
                    CodigoProductoEncontrado = LD.GetInt64(LD.GetOrdinal("CodigoProducto"))
                    DescripcionProductoEncontrado = LD.GetString(LD.GetOrdinal("DescripcionProducto"))

                    DGVDetalle_Factura.Rows.Add(CodigoProductoEncontrado, DescripcionProductoEncontrado)

                End If
            End Using

        Catch ex As Exception
            MsgBox("Error al intentar Cargar los Datos de los Controles",
           vbExclamation, "Error")
            MessageBox.Show(ex.Message) ' Muestra el Mensaje de Error.
        End Try

The problem is that the Code only fills one row of the datagridview

Sin título02

But according to the information in the table, it should have turned out like this:

Sin título03

I hope you can help me, thanks in advance.

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,401 questions
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 4,170 Reputation points

    this part

    If LD.Read() Then
    End If

    Change like this

    While LD.Read()
    End While
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Viorel 106.4K Reputation points


    0 comments No comments

  2. ELIRDFUL 100 Reputation points
    Solved 100x%
    Thank you….
    0 comments No comments