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

ELIRDFUL 185 Reputation points
2023-11-11T19:52:46.4266667+00:00

Hello everyone.

 I have a datagridview, designed with two specific columns.

Sin título

with this code I try to fill the columns, predesigned

Try
            Using Conexion As New SqlConnection(cadenaconexion)
                Conexion.Open()
                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)
                'LD=LectorDatos
                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.

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

Accepted answer
  1. KOZ6.0 6,395 Reputation points
    2023-11-11T20:01:05.4466667+00:00

    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 116.6K Reputation points
    2023-11-11T20:01:16.55+00:00

    (Removed)

    0 comments No comments

  2. ELIRDFUL 185 Reputation points
    2023-11-11T22:10:29.79+00:00
    Solved 100x%
    
    Thank you….
    
    0 comments No comments

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.