Foros aprenderaprogramar.com

Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: marcoslopez en 15 de Febrero 2017, 16:20

Título: actualizar datagridview desde textbox Visual Basic trasladar cambios base datos
Publicado por: marcoslopez en 15 de Febrero 2017, 16:20
Buen día, gracias de antemano por su ayuda

Tengo una tabla con codigo, clave, descripcion, precio

y tengo un formulario y busco desde un textbox la clave y me trae los datos a los texbox

me trae codigo, descripcion,precio y tengo otros textbox donde tecleo cantidad y descuento

al insertar descuento y cantidad el boton ingresar me hace el calculo de total precio*cantidad-descuento y lo inserto en un datagridview
y ahí sigo insertando mas datos.

Mi problema es que seleciono una fila del datagidview y me pasa los datos de la fila a los texbox
Estos quiero modificarlos por jemplo para cambiar la cantidad o el descuento,precio y que los modifique en la misma fila del datagridview
Tengo este código al seleccionar la fila en el datagrid me pasa los datos a los textbox

Código: [Seleccionar]
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick, DataGridView1.CellClick

        'al seleccionar una fila manda los datos a los texbox
        Dim i As Integer
        i = DataGridView1.CurrentRow.Index


        txtcantidad.Text = DataGridView1.Item(0, i).Value()
        txtclave.Text = DataGridView1.Item(1, i).Value()
        txtdescripcion.Text = DataGridView1.Item(2, i).Value()
        txtlista.Text = DataGridView1.Item(3, i).Value()
        txtprecio.Text = DataGridView1.Item(4, i).Value()
        txtdescuento.Text = DataGridView1.Item(5, i).Value()
        txttotal.Text = DataGridView1.Item(6, i).Value()


    End Sub



y este es el codigo para insertar los datos que busco de la base de datos

Código: [Seleccionar]
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btinsertar.Click
        If txtclave.Text = "" Then  'SI EL TEXBOX DE CODIGO ESTA VACIO QUE MANDE MENSAJE Y NO INGRESE NADA AL DATAGRID
            MsgBox("DEVE INGRESAR CODIGO")
            txtclave.Select()  'ASIGNA EL FOCUS AL TXT CODIGO
        Else


            row(0) = txtcantidad.Text
            row(1) = txtclave.Text
            row(2) = txtdescripcion.Text
            row(3) = txtlista.Text
            row(4) = txtprecio.Text
            row(5) = txtdescuento.Text
            row(6) = Val(txtcantidad.Text) * Val(txtprecio.Text) 'INSERTA LA MULTIPLICACION DEL PRECIO UNITARIO Y LA CANTIDAD

            Dim rowToSave As String() = New String() {row(1), row(2), row(3), row(4), row(5), row(6)}
            DataGridView1.Rows.Add(row)

            txtcantidad.Text = ""
            txtclave.Text = ""
            txtdescripcion.Text = ""
            txtprecio.Text = ""
            txtdescuento.Text = ""
            txtlista.Text = ""
            txttotal.Text = ""
        End If

        'PARA SUMAR LA COLUMNA DE TOTALES
        Dim Total As Single
        Dim cont As Integer = 0 'PARA CONTADOR DE PARTIDAS
        Dim Col As Integer = Me.DataGridView1.CurrentCell.ColumnIndex
        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            Total += Val(row.Cells(6).Value) 'ROW.CELLS (NUMERO DE LA COLUMNA A SUMAR).VALUE
        Next
        Me.txttotalgral.Text = Total.ToString

        'PARA CONTAR EL NUMERO DE PARTIDAS
        For Each r As DataGridViewRow In DataGridView1.Rows
            'en cells() va el indice de la columna que quiere verificar que tenga datos
            If Not r.Cells(0).Value = "" Then
                cont += 1
            End If
        Next
        ' MsgBox("las filas con registros son: " & cont)
        txtpartidas.Text = cont 'PASA EL DATO A UN TEXBOX
    End Sub


Espero me puedan orientar con este detalle gracias
Título: Re:actualizar datagridview desde textbox Visual Basic trasladar cambios base datos
Publicado por: Ogramar en 14 de Abril 2017, 20:30
Buenas

Para escribir en los foros conviene seguir las indicaciones que se dan en https://www.aprenderaprogramar.com/foros/index.php?topic=1460.0

Para el problema descrito veo dos opciones:

a) Usar un botón para actualizar el datagrid después de modificar los textbox

b) Usar el evento change para actualizar ante cualquier cambio

La cabecera sería algo como:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged


Salu2
Título: Re:actualizar datagridview desde textbox Visual Basic trasladar cambios base datos
Publicado por: marcoslopez en 17 de Abril 2017, 22:54
muchas gracias por tu respuesta Ogramar

mi problema es por ejemplo


al ingresar varios producto en un datagrid, la linea 5 del datagrid quiero modificar la cantidad y selecciono la linea 5 el me manda todos los datos a unos texbox y modifico pero al dar guardar nuevamente se ingresa una nueva linea lo que quiero es que se actualice en el datagrig solo la linea 5 con la cantidad que actualice y que no agregue una nueva de antemano gracias
Título: Re:actualizar datagridview desde textbox Visual Basic trasladar cambios base datos
Publicado por: Ogramar en 18 de Abril 2017, 19:01
Buenas esto podría ser un ejemplo donde se actualizan ciertos campos a partir de valores contenidos en textboxes:

Código: [Seleccionar]
Dim i = subGridView.CurrentRow.Index

With subGridView
                .Rows(i).Cells("Material").Value = txtMaterial.Text
                .Rows(i).Cells("Description").Value = txtDescription.Text
                .Rows(i).Cells("UoM").Value = txtUoM.Text
                .Rows(i).Cells("Quantity").Value = txtQuantity.Text
                .Rows(i).Cells("Weight").Value = txtWeight.Text
                .Rows(i).Cells("Price").Value = txtPrice.Text
                .Rows(i).Cells("Total_Weight").Value = txtTotalWeight.Text
                .Rows(i).Cells("Total_Price").Value = txtTotalPrice.Text
End With
Título: Re:actualizar datagridview desde textbox Visual Basic trasladar cambios base datos
Publicado por: marcoslopez en 21 de Abril 2017, 20:38
muchas gracias ogramar eso me funciono solo me falta validar
pero tu ejemplo es justo lo que no me salia
y así actualizo en la misma celda sin insertar una nueva