Update datagridview control in c# with Access Data Source

28 Views Asked by At

**I am programming in c#, and I have a problem with a datagridview. The datagridview data source is an Access table, and it allows me to add new records without problems, but it does not allow me to modify records. When I modify the value of a cell in the datagridview, the record in Access is not updated.

Please, I don't know what I could be doing wrong.

This is the code I use to perform the UPDATE**

    private void mainDatosDgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
            string CampoAnterior = Globales.ValorAntesEditar;

            DataGridViewRow updRow = mainDatosDgv.Rows[e.RowIndex];
            string CampoNuevo = updRow.Cells["Operario"].Value.ToString();

            string consulta = "UPDATE Operarios SET Operario = @ValorDespuesEditar WHERE Operario = @ValorAntesEditar";
            using (OleDbConnection conexion = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Pizarra\Pizarra.accdb;"))
            {
                try
                {
                    conexion.Open();
                    using (OleDbCommand cmd = new OleDbCommand(consulta, conexion))
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@ValorAntesEditar", Globales.ValorAntesEditar);
                        cmd.Parameters.AddWithValue("@ValorDespuesEditar", CampoNuevo);
                        cmd.ExecuteNonQuery();
                    }
                    conexion.Close();
                }
                catch (Exception ex)
                {
                }
            }
        }
        mainDatosDgv.Refresh();
    }

I need help with the above code to solve the problem

0

There are 0 best solutions below