• Lunes 29 de Abril de 2024, 14:26

Autor Tema:  Actualizar datos de datagrid con visual basic 2005 y mysql.  (Leído 2282 veces)

elultimoguerrero

  • Nuevo Miembro
  • *
  • Mensajes: 19
    • Ver Perfil
Actualizar datos de datagrid con visual basic 2005 y mysql.
« en: Lunes 7 de Septiembre de 2009, 06:12 »
0
Hola amigos de solocodigo, espero me puedan ayudar con la siguiente duda:
Estoy haciendo un sistema de control de inventario de un videoclub en visual basic 2005 y mysql, mis registros los guarda a la perfección pero si yo quiero modificar esos datos y doy clic al botón guardar modificación no me permite hacerlo, allí dejé mi código de guardar modificación en comentarios, espero me puedan ayudar a formular el código correcto, gracias.

Imports MySql.Data.MySqlClient
Imports System.Data

Public Class rentadepeliculas
    Dim con As New MySqlConnection
    Dim ds As New DataSet
    Private Sub borrarcajas()
        tnocredencial.Text = ""
        tfolio.Text = ""
        dfecha.Value = Date.Now
        tnombre.Text = ""
        tdireccion.Text = ""
        ttelefono.Text = ""
        DataGridView1.Rows.Clear()
        DataGridView1.Rows.Add(10)
        tcantidadconletra.Text = ""
        tsubtotal.Text = ""
        tiva.Text = ""
        ttotal.Text = ""
    End Sub
    Private Sub habilitarcajas()
        tnocredencial.Enabled = True
        dfecha.Enabled = True
        DataGridView1.Enabled = True
    End Sub
    Private Sub deshabilitarcajas()
        tnocredencial.Enabled = False
        dfecha.Enabled = True
        DataGridView1.Enabled = False
    End Sub

    Private Sub rentadepeliculas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con = New MySqlConnection("server=X;database=bdX;uid=X;pwd=XXXXX")
        con.Open()
        DataGridView1.Rows.Add(10)
    End Sub

    Private Sub lguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lguardar.Click
        Dim cnrm As New MySqlDataAdapter
        Dim cnrd As New MySqlDataAdapter
        Dim dsrentas As New DataSet
        Dim filarentas As DataRow
        Dim sincrentas As New MySqlCommandBuilder
        Dim comrentas As New MySqlCommand
        cnrm = New MySqlDataAdapter("select * from rentadepeliculas_maestro order by Folio;", con)
        cnrm.Fill(dsrentas, "rentadepeliculas_maestro")
        sincrentas = New MySqlCommandBuilder(cnrm)
        If tnocredencial.Text = "" Then
            MsgBox("Es necesario que ingrese el número de credencial.", MsgBoxStyle.Information, "Operación cancelada.")
            Exit Sub
        End If
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(i).Cells(0).Value = "" And DataGridView1.Rows(i).Cells(4).Value = "" And DataGridView1.Rows(i).Cells(5).Value = "" And tcantidadconletra.Text = "" And tsubtotal.Text = "" And tiva.Text = "" And ttotal.Text = "" Then
                MsgBox("No puede realizar un registro de renta con campos en blanco.", MsgBoxStyle.Information, "Operación cancelada.")
                Exit Sub
            End If
        Next
        If dsrentas.Tables("rentadepeliculas_maestro").Rows.Count > 0 Then
            filarentas = dsrentas.Tables("rentadepeliculas_maestro").Rows(dsrentas.Tables("rentadepeliculas_maestro").Rows.Count - 1)
            tfolio.Text = Val(filarentas("Folio")) + 1
        Else
            tfolio.Text = "1"
        End If
        filarentas = dsrentas.Tables("rentadepeliculas_maestro").NewRow
        filarentas("No_credencial") = tnocredencial.Text
        filarentas("Folio") = tfolio.Text
        filarentas("Fecha") = dfecha.Text
        filarentas("Nombre") = tnombre.Text
        filarentas("Direccion") = tdireccion.Text
        filarentas("Telefono") = ttelefono.Text
        filarentas("Cantidad_letra") = tcantidadconletra.Text
        filarentas("Subtotal") = Val(tsubtotal.Text)
        filarentas("Iva") = Val(tiva.Text)
        filarentas("Total") = Val(ttotal.Text)
        dsrentas.Tables("rentadepeliculas_maestro").Rows.Add(filarentas)
        cnrm.Update(dsrentas.Tables("rentadepeliculas_maestro"))
        dsrentas.AcceptChanges()
        cnrd = New MySqlDataAdapter("select * from rentadepeliculas_detalle order by Num;", con)
        cnrd.Fill(dsrentas, "rentadepeliculas_detalle")
        sincrentas = New MySqlCommandBuilder(cnrd)
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(i).Cells(1).Value <> "" Then
                filarentas = dsrentas.Tables("rentadepeliculas_detalle").NewRow
                filarentas("Folio") = tfolio.Text
                filarentas("Ean") = DataGridView1.Rows(i).Cells(0).Value
                filarentas("Titulo") = DataGridView1.Rows(i).Cells(1).Value
                filarentas("Formato") = DataGridView1.Rows(i).Cells(2).Value
                filarentas("Contenido") = CDbl(DataGridView1.Rows(i).Cells(3).Value)
                filarentas("Precio") = CDbl(DataGridView1.Rows(i).Cells(4).Value)
                filarentas("Copias") = CDbl(DataGridView1.Rows(i).Cells(5).Value)
                filarentas("Importe") = CDbl(DataGridView1.Rows(i).Cells(6).Value)
                dsrentas.Tables("rentadepeliculas_detalle").Rows.Add(filarentas)
                cnrd.Update(dsrentas.Tables("rentadepeliculas_detalle"))
                dsrentas.AcceptChanges()
            End If
        Next
        deshabilitarcajas()
        lnuevo.Enabled = True
        lguardar.Enabled = False
        limprimir.Enabled = True
        lbuscar.Enabled = True
        lmodificar.Enabled = True
        lguardarmodificacion.Enabled = False
        leliminar.Enabled = False
        lcancelar.Enabled = False
    End Sub

    Private Sub lbuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbuscar.Click
        Dim cnrentas As New MySqlDataAdapter
        Dim dsrent As New DataSet
        Dim filarent As DataRow
        Dim x As String
        x = InputBox("Introduzca el número de folio que desea buscar.")
        If x = "" Then Exit Sub
        cnrentas = New MySqlDataAdapter("select * from rentadepeliculas_maestro where Folio = " & x & ";", con)
        cnrentas.Fill(dsrent, "rentadepeliculas_maestro")
        If dsrent.Tables("rentadepeliculas_maestro").Rows.Count > 0 Then
            filarent = dsrent.Tables("rentadepeliculas_maestro").Rows(0)
            tnocredencial.Text = filarent("No_credencial")
            tfolio.Text = filarent("Folio")
            dfecha.Text = filarent("Fecha")
            tnombre.Text = filarent("Nombre")
            tdireccion.Text = filarent("Direccion")
            ttelefono.Text = filarent("Telefono")
            tcantidadconletra.Text = filarent("Cantidad_letra")
            tsubtotal.Text = filarent("Subtotal")
            tiva.Text = filarent("Iva")
            ttotal.Text = filarent("Total")
        Else
            MsgBox("Folio no encontrado.", MsgBoxStyle.Information, "Operación cancelada.")
            borrarcajas()
            lnuevo.Enabled = True
            lguardar.Enabled = False
            limprimir.Enabled = False
            lbuscar.Enabled = True
            lmodificar.Enabled = False
            lguardarmodificacion.Enabled = False
            leliminar.Enabled = False
            lcancelar.Enabled = False
            Exit Sub
            dsrent.Clear()
            dsrent.Dispose()
            cnrentas.Dispose()
        End If
        DataGridView1.Rows.Clear()
        DataGridView1.Rows.Add(10)
        cnrentas = New MySqlDataAdapter("select * from rentadepeliculas_detalle where Folio =" & x & ";", con)
        cnrentas.Fill(dsrent, "rentadepeliculas_detalle")
        If dsrent.Tables("rentadepeliculas_detalle").Rows.Count > 0 Then
            For i As Integer = 0 To dsrent.Tables("rentadepeliculas_detalle").Rows.Count - 1
                filarent = dsrent.Tables("rentadepeliculas_detalle").Rows(i)
                DataGridView1.Rows(i).Cells(0).Value = filarent("Ean")
                DataGridView1.Rows(i).Cells(1).Value = filarent("Titulo")
                DataGridView1.Rows(i).Cells(2).Value = filarent("Formato")
                DataGridView1.Rows(i).Cells(3).Value = filarent("Contenido")
                DataGridView1.Rows(i).Cells(4).Value = filarent("Precio")
                DataGridView1.Rows(i).Cells(5).Value = filarent("Copias")
                DataGridView1.Rows(i).Cells(6).Value = filarent("Importe")
                lnuevo.Enabled = False
                lguardar.Enabled = False
                limprimir.Enabled = True
                lbuscar.Enabled = True
                lmodificar.Enabled = True
                lguardarmodificacion.Enabled = False
                leliminar.Enabled = True
                lcancelar.Enabled = True
            Next
        End If
        dsrent.Clear()
        dsrent.Dispose()
        cnrentas.Dispose()
    End Sub

    Private Sub lmodificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lmodificar.Click
        habilitarcajas()
        tnocredencial.Focus()
        lnuevo.Enabled = False
        lguardar.Enabled = False
        limprimir.Enabled = False
        lbuscar.Enabled = False
        lmodificar.Enabled = False
        lguardarmodificacion.Enabled = True
        leliminar.Enabled = False
        lcancelar.Enabled = True
    End Sub

    Private Sub lguardarmodificacion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lguardarmodificacion.Click
        'Dim cnrm As New MySqlDataAdapter
        'Dim cnrd As New MySqlDataAdapter
        'Dim dsrentas As New DataSet
        'Dim filarentas As DataRow
        'Dim sincrentas As New MySqlCommandBuilder
        'Dim comrentas As New MySqlCommand
        'cnrm = New MySqlDataAdapter("select * from rentadepeliculas_maestro order by Folio;", con)
        'cnrm.Fill(dsrentas, "rentadepeliculas_maestro")
        'sincrentas = New MySqlCommandBuilder(cnrm)
        'filarentas = dsrentas.Tables("rentadepeliculas_maestro").NewRow
        'filarentas.BeginEdit()
        'filarentas("No_credencial") = tnocredencial.Text
        'filarentas("Folio") = tfolio.Text
        'filarentas("Fecha") = dfecha.Text
        'filarentas("Nombre") = tnombre.Text
        'filarentas("Direccion") = tdireccion.Text
        'filarentas("Telefono") = ttelefono.Text
        'filarentas("Cantidad_letra") = tcantidadconletra.Text
        'filarentas("Subtotal") = Val(tsubtotal.Text)
        'filarentas("Iva") = Val(tiva.Text)
        'filarentas("Total") = Val(ttotal.Text)
        'filarentas.EndEdit()
        'cnrm.Update(dsrentas.Tables("rentadepeliculas_maestro"))
        'dsrentas.AcceptChanges()
        'cnrd = New MySqlDataAdapter("select * from rentadepeliculas_detalle order by Num;", con)
        'cnrd.Fill(dsrentas, "rentadepeliculas_detalle")
        'sincrentas = New MySqlCommandBuilder(cnrd)
        'For i As Integer = 0 To DataGridView1.Rows.Count - 1
        '    If DataGridView1.Rows(i).Cells(1).Value <> "" Then
        '        filarentas = dsrentas.Tables("rentadepeliculas_detalle").NewRow
        '        filarentas.BeginEdit()
        '        filarentas("Folio") = tfolio.Text
        '        filarentas("Ean") = DataGridView1.Rows(i).Cells(0).Value
        '        filarentas("Titulo") = DataGridView1.Rows(i).Cells(1).Value
        '        filarentas("Formato") = DataGridView1.Rows(i).Cells(2).Value
        '        filarentas("Contenido") = CDbl(DataGridView1.Rows(i).Cells(3).Value)
        '        filarentas("Precio") = CDbl(DataGridView1.Rows(i).Cells(4).Value)
        '        filarentas("Copias") = CDbl(DataGridView1.Rows(i).Cells(5).Value)
        '        filarentas("Importe") = CDbl(DataGridView1.Rows(i).Cells(6).Value)
        '        filarentas.EndEdit()
        '        cnrm.Update(dsrentas.Tables("rentadepeliculas_detalle"))
        '        dsrentas.AcceptChanges()
        '    End If
        'Next
        'deshabilitarcajas()
        'lnuevo.Enabled = True
        'lguardar.Enabled = False
        'limprimir.Enabled = True
        'lbuscar.Enabled = True
        'lmodificar.Enabled = True
        'lguardarmodificacion.Enabled = False
        'leliminar.Enabled = False
        'lcancelar.Enabled = False
    End Sub

    Private Sub tnocredencial_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tnocredencial.KeyPress
        Dim cncliente As New MySqlDataAdapter
        Dim dscliente As New DataSet
        Dim filacliente As DataRow
        ''If Asc(e.KeyChar) = Keys.Return Then
        If Asc(e.KeyChar) = 13 Then
            cncliente = New MySqlDataAdapter("select Nombres, Apellido_paterno, Apellido_materno, Colonia, Calle, Ciudad, Telefono from registrodeclientes where No_credencial='" & tnocredencial.Text & "';", con)
            cncliente.Fill(dscliente, "registrodeclientes")
            If dscliente.Tables("registrodeclientes").Rows.Count > 0 Then
                filacliente = dscliente.Tables("registrodeclientes").Rows(0)
                tnombre.Text = filacliente("Nombres") & " " & filacliente("Apellido_paterno") & " " & filacliente("Apellido_materno")
                tdireccion.Text = filacliente("Colonia") & " " & filacliente("Calle") & " " & filacliente("Ciudad")
                ttelefono.Text = filacliente("Telefono")
            Else
                MsgBox("Cliente no encontrado.", MsgBoxStyle.Information, "Búsqueda de clientes.")
                tnocredencial.Text = ""
                tnombre.Text = ""
                tdireccion.Text = ""
                ttelefono.Text = ""
            End If
            dscliente.Clear()
            dscliente.Dispose()
            cncliente.Dispose()
            'dscliente.Tables("registrodeclientes").Clear()
        End If
    End Sub

    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        Dim cnpel As New MySqlDataAdapter
        Dim dspel As New DataSet
        Dim filapel As DataRow
        Dim ean As String
        Dim precio As Double
        Dim copias As Double
        Dim vsubt As Double = 0
        Dim vean As String = ""
        Dim vlinea As Integer
        Select Case e.ColumnIndex
            Case 0
                vean = DataGridView1.CurrentCell.Value
                vlinea = DataGridView1.CurrentRow.Index
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
                    If vean = DataGridView1.Rows(i).Cells(0).Value And i <> vlinea Then
                        MsgBox("No puede introducir un mismo ean.", MsgBoxStyle.Information, "Operación cancelada.")
                        DataGridView1.CurrentCell.Value = ""
                        SendKeys.Send("{up}")
                        Exit Sub
                    End If
                Next
                ean = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value
                cnpel = New MySqlDataAdapter("select Titulo, Formato, Contenido from registrodepeliculas where Ean='" & ean & "';", con)
                cnpel.Fill(dspel, "regpeliculas")
                If dspel.Tables("regpeliculas").Rows.Count > 0 Then
                    filapel = dspel.Tables("regpeliculas").Rows(0)
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = filapel("titulo")
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value = filapel("formato")
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = filapel("contenido")
                Else
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = ""
                End If
                dspel.Clear()
                dspel.Dispose()
                cnpel.Dispose()
                SendKeys.Send("{up}")
                SendKeys.Send("{tab}")
                SendKeys.Send("{tab}")
                SendKeys.Send("{tab}")
                SendKeys.Send("{tab}")
            Case 4
                copias = CDbl(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(4).Value)
                If copias > 0 Then
                    SendKeys.Send("{up}")
                    SendKeys.Send("{tab}")
                Else
                    SendKeys.Send("{up}")
                End If
            Case 5
                precio = CDbl(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(4).Value)
                copias = CDbl(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(5).Value)
                If precio > 0 And copias > 0 Then
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(6).Value = precio * copias
                    For i As Integer = 0 To DataGridView1.Rows.Count - 1
                        vsubt = vsubt + CDbl(DataGridView1.Rows(i).Cells(6).Value)
                    Next
                    tsubtotal.Text = vsubt
                    tiva.Text = vsubt * 0.15
                    ttotal.Text = CDbl(tsubtotal.Text) + CDbl(tiva.Text)
                    SendKeys.Send("{up}")
                    SendKeys.Send("{tab}")
                    SendKeys.Send("{tab}")
                End If
        End Select
    End Sub
End Class

elultimoguerrero

  • Nuevo Miembro
  • *
  • Mensajes: 19
    • Ver Perfil
Re: Actualizar datos de datagrid con visual basic 2005 y mysql.
« Respuesta #1 en: Domingo 13 de Septiembre de 2009, 22:30 »
0
Hola amigos de solocodigo.com, después de intentar varios códigos hé encontrado la solución a la duda que tenía sobre como modificar los registros de un datagrid y guardarlos en su respectiva base de datos, el código sólo tenía un par de líneas de código incorrecto, así que sólo les pongo el código de guardar modificación, espero les sea de utilidad:

    Private Sub lguardarmodificacion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lguardarmodificacion.Click
        Dim cnrm As New MySqlDataAdapter
        Dim cnrd As New MySqlDataAdapter
        Dim dsrentas As New DataSet
        Dim filarentas As DataRow
        Dim sincrentas As New MySqlCommandBuilder
        Dim comrentas As New MySqlCommand
        cnrm = New MySqlDataAdapter("select * from rentadepeliculas_maestro where folio=" & tfolio.Text & " order by Folio;", con)
        cnrm.Fill(dsrentas, "rentadepeliculas_maestro")
        sincrentas = New MySqlCommandBuilder(cnrm)
        filarentas = dsrentas.Tables("rentadepeliculas_maestro").Rows(0)
        filarentas.BeginEdit()
        filarentas("No_credencial") = tnocredencial.Text
        filarentas("Folio") = tfolio.Text
        filarentas("Fecha") = dfecha.Text
        filarentas("Nombre") = tnombre.Text
        filarentas("Direccion") = tdireccion.Text
        filarentas("Telefono") = ttelefono.Text
        filarentas("Cantidad_letra") = tcantidadconletra.Text
        filarentas("Subtotal") = Val(tsubtotal.Text)
        filarentas("Iva") = Val(tiva.Text)
        filarentas("Total") = Val(ttotal.Text)
        filarentas.EndEdit()
        cnrm.Update(dsrentas.Tables("rentadepeliculas_maestro"))
        dsrentas.AcceptChanges()
        cnrd = New MySqlDataAdapter("select * from rentadepeliculas_detalle where folio=" & tfolio.Text & " order by Num;", con)
        cnrd.Fill(dsrentas, "rentadepeliculas_detalle")
        sincrentas = New MySqlCommandBuilder(cnrd)
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(i).Cells(1).Value <> "" Then
                filarentas = dsrentas.Tables("rentadepeliculas_detalle").Rows(i)
                filarentas.BeginEdit()
                filarentas("Folio") = tfolio.Text
                filarentas("Ean") = DataGridView1.Rows(i).Cells(0).Value
                filarentas("Titulo") = DataGridView1.Rows(i).Cells(1).Value
                filarentas("Formato") = DataGridView1.Rows(i).Cells(2).Value
                filarentas("Contenido") = CDbl(DataGridView1.Rows(i).Cells(3).Value)
                filarentas("Precio") = CDbl(DataGridView1.Rows(i).Cells(4).Value)
                filarentas("Copias") = CDbl(DataGridView1.Rows(i).Cells(5).Value)
                filarentas("Importe") = CDbl(DataGridView1.Rows(i).Cells(6).Value)
                filarentas.EndEdit()
                cnrd.Update(dsrentas.Tables("rentadepeliculas_detalle"))
                dsrentas.AcceptChanges()
            End If
        Next
        deshabilitarcajas()
        lnuevo.Enabled = True
        lguardar.Enabled = False
        limprimir.Enabled = True
        lbuscar.Enabled = True
        lmodificar.Enabled = True
        lguardarmodificacion.Enabled = False
        leliminar.Enabled = False
        lcancelar.Enabled = False
    End Sub