Visual Basic.NET - agregar filas en datagridview vb.net

 
Vista:
sin imagen de perfil
Val: 1
Ha disminuido su posición en 2 puestos en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

agregar filas en datagridview vb.net

Publicado por gabriel (1 intervención) el 17/03/2019 19:06:35
Estoy haciendo un sistema de punto de venta, con lector de código de barras, cuando hago mi consulta a mi bd los cargo en un datagridview si hay lecturas con códigos de barras iguales en la columna cantidad se va sumando automáticamente, mi problema surge cuando el código de barra es diferene a los que ya están cargados en el datagridview en la primera lectura del segundo articulo de código de barra diferente si lo agrega en una fila nueva, pero al agregar otro segundo articulo lo reescribe en la primera fila del datagridview, y se pierde los datos que voy agregando a mi lista de ventas, agradecería sus ayuda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
If (e.KeyChar = (Convert.ToChar(Keys.Enter))) Then
    Dim conexion As SqlConnection
    conexion = New SqlConnection("Data Source=Gabriel-PC\SQLEXPRESS;Initial Catalog=Ventas;Integrated Security=True")
    conexion.Open()
    Dim cod As String = txt_No_Factura.Text
    Dim cadena As String = "select codigo_barra,idproducto,nombre,precio_venta from producto where codigo_barra ='" & cod & "'"
    Dim comando As SqlCommand
    comando = New SqlCommand(cadena, conexion)
    Dim registro As SqlDataReader
    registro = comando.ExecuteReader()
    registro.Read()
 
 
    cod_barra.Text = registro(0)
    txt_idproducto.Text = registro(1)
    txt_nombreProducto.Text = registro(2)
    txt_precioUnitario.Text = registro(3)
 
    datalistado.ColumnCount = 5
    With datalistado
        .Columns(0).Name = "Cantidad"
        .Columns(1).Name = "Codigo Barra"
        .Columns(2).Name = "Codigo Producto"
        .Columns(3).Name = "Nombre"
        .Columns(4).Name = "Precio de Venta"
    End With
    conexion.Close()
 
    If datalistado.Rows.Count = Nothing Then
        cantidad += 1
        datalistado.Rows.Add()
        datalistado.Rows(f).Cells(0).Value = cantidad.ToString() - 1
        datalistado.Rows(f).Cells(1).Value = cod_barra.Text
        datalistado.Rows(f).Cells(2).Value = txt_idproducto.Text
        datalistado.Rows(f).Cells(3).Value = txt_nombreProducto.Text
        datalistado.Rows(f).Cells(4).Value = txt_precioUnitario.Text
        'f = f + 1
        txt_No_Factura.Text = ""
    End If
 
    If datalistado.Rows.Count > 0 Then
        For fila As Integer = 0 To datalistado.Rows.Count - 1
 
            datalistado.CurrentCell = datalistado.Rows(fila).Cells(1)
            If datalistado.Rows(fila).Cells(1).Value = cod_barra.Text Then 'cod.toString()
                existe = True
            End If
            If datalistado.Rows(fila).Cells(1).Value <> cod_barra.Text Then
                existe = False
            End If
            Label15.Text = fila.ToString()
        Next
 
    End If
 
    If existe = True Then
        'f = f + 1
        cantidad += 1
        datalistado.Rows(fila).Cells(0).Value = (cantidad.ToString() - 1)
        datalistado.Rows(fila).Cells(1).Value = cod_barra.Text
        datalistado.Rows(fila).Cells(3).Value = txt_nombreProducto.Text
        datalistado.Rows(fila).Cells(4).Value = txt_precioUnitario.Text
        txt_No_Factura.Text = "" ' agregue esto
        'f = f + 1
    End If
    If existe = False Then
        datalistado.Rows.Add(txt_cantiad.Text, cod_barra.Text, txt_idproducto.Text, txt_nombreProducto.Text, txt_precioUnitario.Text)
        'f = f + 1
        txt_No_Factura.Text = ""
    End If
End If
Catch ex As Exception
MessageBox.Show("El articulo no fue registrado cuando ingreso a almacen")
End Try
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Phil Rob
Val: 3.353
Oro
Ha mantenido su posición en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

agregar filas en datagridview vb.net

Publicado por Phil Rob (1554 intervenciones) el 17/03/2019 22:39:25
Hola,

Tienes que revisar tu algorimca, especialmente los codigos siguiente :
1
2
3
4
5
6
7
8
9
10
11
For fila As Integer = 0 To datalistado.Rows.Count - 1
    datalistado.CurrentCell = datalistado.Rows(fila).Cells(1)
    If datalistado.Rows(fila).Cells(1).Value = cod_barra.Text Then 'cod.toString()
        existe = True
  '  End If
ELSE
  '  If datalistado.Rows(fila).Cells(1).Value <> cod_barra.Text Then
        existe = False
    End If
    Label15.Text = fila.ToString()
Next

Ves mi ejemplo :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim IndexColona As Integer
    Dim IndexLinea As Integer
    ' datalistado.ClearSelection()
    IndexColona = datalistado.Columns("Producto").Index
    For IndexLinea = 0 To MiDGV.Rows.Count - 1
        If Not datalistado.Item(IndexColona, IndexLinea).Value Is Nothing Then
            If datalistado.Item(IndexColona, IndexLinea).Value = TextBox1.Text Then
                Exit Sub
            End If
        End If
    Next
    datalistado.Rows.Add(TextBox2.Text, TextBox1.Text)   'TextBox1 es "Producto" y TextBox2 es "Cantidad"
End Sub
Tambien ves el video : https://www.dropbox.com/s/n49v7tmibovqxya/AgregarLinea.mp4?dl=0
Espero que esto te ayudará ...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar