Add data to DataGridView without deleting the previous data

1

I'm making a point of sale with SQL Server 2012 connection but I had a problem. When I read the code of a product, it connects to the database and the product is added to the DataGridView , but when I read another product, the one I already had is deleted and the new one is added. I've been reading that there are linked and unlinked tables, dataset , columns , rows , etc

Sub consultaproducto(ByVal bb As String, ByVal dgv As DataGridView)
        Try
            adaptador = New SqlDataAdapter("select * from producto where nombre like '" & bb + "%" & "'", cn)
            dt = New DataTable
            adaptador.Fill(dt)
            dgv.DataSource = dt
        Catch ex As Exception
            MsgBox("Problemas al conectar con al base de datos ")
        End Try
    End Sub

What I want is that by continuing reading the barcode of the products, we continue to add products in DataGridView .

    
asked by ivangarciamar 31.05.2017 в 21:37
source

1 answer

2

The solution to your problem can be done using a DataTable declared in the code outside the queryproduct procedure, and using the code of the procedure that you wrote (that works for you and brings you the information) to import the row obtained into that DataTable.

 Dim dt As DataTable

    Private Sub consultaproducto(ByVal bb As String, ByVal dgv As DataGridView)
        Try

            ' Creo el DataTable que llenaremos con Fill

            Dim DT_ImportoProducto As DataTable
            Dim adaptador = New SqlDataAdapter("select * from producto where nombre like '" & bb + "%" & "'", New MySqlConnection(cMOSTRAR_String))

            ' Llenamos el DataTable donde traemos el producto

            adaptador.Fill(DT_ImportoProducto)

            ' Si el DT posee más de 0 filas
            If DT_ImportoProducto.Rows.Count <> 0 Then
                ' Porcion de Código para formatear el DataTable donde 
                ' almacenaremos todos los productos.
                If dt Is Nothing Then
                    ' Copio la estructura (columnas), del resultado obtenido
                    dt = DT_ImportoProducto.Clone
                End If
                ' Importo la Fila obtenida
                dt.ImportRow(DT_ImportoProducto.Rows(0))
            End If
            dgv.DataSource = dt
        Catch ex As Exception
            MsgBox("Problemas al conectar con al base de datos ")
        End Try
    End Sub
    
answered by 01.06.2017 в 20:23