none
CODIGO PARA EDITAR LA INFORMACIÓN YA ESCRITA EN EL DATAGRIDVIEW

    Pregunta

  • Buenas tarde y como el titulo ya lo dice me gustaría hacer un botón que al presionarlo puedes editar la información ya grabada en el DataGridView y te lo vuelva a guardar sin problemas o si se puede de preferencia un mismo botón que te guarde tus nuevos datos y te vuelva a guardar tus mismo datos ya creados sin necesidad de errores algo como se muestra en la imagen siguiente 

    después de agregarlo y guardarlo y cometiste un error en un dato lo puedas corregir y volver a guardar nuevamente

    de antemanos se les agradese

    un cordial saludo...


    miércoles, 21 de diciembre de 2016 19:32

Respuestas

  • Hola:
    En un Form como el de la imagen, copia y pega el siguiente código

    Option Explicit On
    Option Strict Off
    Imports System.Data.OleDb
    Public Class Form1
        Private msCadenaAccess As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Northwind.accdb"
        Private lsQuery As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Me.txtCustomerID.Enabled = False
            Try
                Dim loDataTable As New DataTable
                Using loConexion As New OleDbConnection(msCadenaAccess)
                    Dim loDataAdapter As New OleDbDataAdapter(lsQuery, loConexion)
                    loDataAdapter.Fill(loDataTable)
                End Using
                Me.DataGridView1.DataSource = loDataTable
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Load", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
        End Sub
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            If Me.DataGridView1.RowCount = 0 Then
                Return
            End If
            Dim loFila As DataGridViewRow = Me.DataGridView1.CurrentRow()
            Me.txtCustomerID.Text = Convert.ToString(loFila.Cells("CustomerID").Value)
            Me.txtCompanyName.Text = Convert.ToString(loFila.Cells("CompanyName").Value)
            Me.txtContactName.Text = Convert.ToString(loFila.Cells("ContactName").Value)
            Me.txtCompanyName.Focus()
        End Sub
        Private Sub btnEditar_Click(sender As Object, e As EventArgs) Handles btnEditar.Click
            Try
                Dim Query As String
                Using loConexion As New OleDbConnection(msCadenaACCESS)
                    Query = "Update Customers Set CompanyName=@CompanyName, ContactName=@ContactName Where CustomerID=@CustomerID"
                    ' crear comando
                    Using loComando As New OleDbCommand(Query, loConexion)
                        ' crear parametro
                        Dim loParametro As OleDbParameter
                        loParametro = New OleDbParameter("@CompanyName", OleDbType.VarChar, 40)
                        loParametro.Value = Me.txtCompanyName.Text
                        loComando.Parameters.Add(loParametro)
                        '
                        loParametro = New OleDbParameter("@ContactName", OleDbType.VarChar, 30)
                        loParametro.Value = Me.txtContactName.Text
                        loComando.Parameters.Add(loParametro)
                        '
                        loParametro = New OleDbParameter("@CustomerID", OleDbType.Char, 5)
                        loParametro.Value = Me.txtCustomerID.Text
                        loComando.Parameters.Add(loParametro)
                        '
                        loConexion.Open()   ' abrir conexión
                        loComando.ExecuteNonQuery()
                    End Using
                End Using
            Catch Exp As Exception
                MessageBox.Show(Exp.Message, "btnEditar_Click", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
            lP_ActualizaGrid()
        End Sub

        Private Sub lP_ActualizaGrid()
            Try
                Using loConexion As New OleDbConnection(msCadenaACCESS)
                    Dim loDataTable As New DataTable
                    Dim loDataAdapter As New OleDbDataAdapter(lsQuery, loConexion)
                    loDataAdapter.Fill(loDataTable)
                    'Para enlazar el datatable con el datagrid, en DataSource se asigna el datatable
                    Me.DataGridView1.DataSource = loDataTable
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message, "lP_ActualizaGrid", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
        End Sub
    End Class

    Un saludo desde Bilbo
    Carlos

    jueves, 22 de diciembre de 2016 8:12

Todas las respuestas

  • Hola:
    En un Form como el de la imagen, copia y pega el siguiente código

    Option Explicit On
    Option Strict Off
    Imports System.Data.OleDb
    Public Class Form1
        Private msCadenaAccess As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Northwind.accdb"
        Private lsQuery As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Me.txtCustomerID.Enabled = False
            Try
                Dim loDataTable As New DataTable
                Using loConexion As New OleDbConnection(msCadenaAccess)
                    Dim loDataAdapter As New OleDbDataAdapter(lsQuery, loConexion)
                    loDataAdapter.Fill(loDataTable)
                End Using
                Me.DataGridView1.DataSource = loDataTable
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Load", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
        End Sub
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            If Me.DataGridView1.RowCount = 0 Then
                Return
            End If
            Dim loFila As DataGridViewRow = Me.DataGridView1.CurrentRow()
            Me.txtCustomerID.Text = Convert.ToString(loFila.Cells("CustomerID").Value)
            Me.txtCompanyName.Text = Convert.ToString(loFila.Cells("CompanyName").Value)
            Me.txtContactName.Text = Convert.ToString(loFila.Cells("ContactName").Value)
            Me.txtCompanyName.Focus()
        End Sub
        Private Sub btnEditar_Click(sender As Object, e As EventArgs) Handles btnEditar.Click
            Try
                Dim Query As String
                Using loConexion As New OleDbConnection(msCadenaACCESS)
                    Query = "Update Customers Set CompanyName=@CompanyName, ContactName=@ContactName Where CustomerID=@CustomerID"
                    ' crear comando
                    Using loComando As New OleDbCommand(Query, loConexion)
                        ' crear parametro
                        Dim loParametro As OleDbParameter
                        loParametro = New OleDbParameter("@CompanyName", OleDbType.VarChar, 40)
                        loParametro.Value = Me.txtCompanyName.Text
                        loComando.Parameters.Add(loParametro)
                        '
                        loParametro = New OleDbParameter("@ContactName", OleDbType.VarChar, 30)
                        loParametro.Value = Me.txtContactName.Text
                        loComando.Parameters.Add(loParametro)
                        '
                        loParametro = New OleDbParameter("@CustomerID", OleDbType.Char, 5)
                        loParametro.Value = Me.txtCustomerID.Text
                        loComando.Parameters.Add(loParametro)
                        '
                        loConexion.Open()   ' abrir conexión
                        loComando.ExecuteNonQuery()
                    End Using
                End Using
            Catch Exp As Exception
                MessageBox.Show(Exp.Message, "btnEditar_Click", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
            lP_ActualizaGrid()
        End Sub

        Private Sub lP_ActualizaGrid()
            Try
                Using loConexion As New OleDbConnection(msCadenaACCESS)
                    Dim loDataTable As New DataTable
                    Dim loDataAdapter As New OleDbDataAdapter(lsQuery, loConexion)
                    loDataAdapter.Fill(loDataTable)
                    'Para enlazar el datatable con el datagrid, en DataSource se asigna el datatable
                    Me.DataGridView1.DataSource = loDataTable
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message, "lP_ActualizaGrid", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
        End Sub
    End Class

    Un saludo desde Bilbo
    Carlos

    jueves, 22 de diciembre de 2016 8:12
  • Gracia me funciono correctamente tu codigo
    jueves, 22 de diciembre de 2016 20:33