J Daniel
J Daniel

Reputation: 13

DataGridView does not update after delete, update or insert

I have a DatagGridView which has a DataSet from an Access database. I have a problem when I delete, update or insert data.

This is an example from my update method.

Private Sub btnactualizar_Click(sender As Object, e As EventArgs) Handles btnactualizar.Click
    novacios()
    Try
        con = New OleDb.OleDbConnection(ruta)
        con.Open()
        Dim actualiza As String = "UPDATE Usuarios SET nombre_real=@a1, correo = @a2, pass = @a3, activo = @a4 WHERE Correo = @a5"
        sentencia = New OleDb.OleDbCommand(actualiza)
        sentencia.Connection = con
        sentencia.Parameters.AddWithValue("@a1", txtusuarios.Text)
        sentencia.Parameters.AddWithValue("@a2", txtmail.Text)
        sentencia.Parameters.AddWithValue("@a3", txtpass.Text)
        sentencia.Parameters.AddWithValue("@a4", txtactivo.Text)
        sentencia.Parameters.AddWithValue("@a5", txtusuarios.Text)
        sentencia.ExecuteReader()
        con.Close()
        MessageBox.Show("Actualización realzada con éxito", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.UsuariosTableAdapter.Fill(Me.Bd_proyectoNDataSet.Usuarios)
        limpiatextos()
    Catch ex As Exception
        ex.Message.ToString()
    End Try
End Sub

As we can see after the messagebox, the list should be updated with the fill method. But the datagrid is still the same.

Here some pictures to understand what I'm talking about. I will appreciate any help.

Before updating the data Here's before update the data. See the datagrid

After updating the data. Nothing happened And here's after I updated the data. Nothing happened

Upvotes: 0

Views: 123

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54457

You're doing things backwards. Don't make a change to the database and then try to update the grid from there. You update the local data first and then save that change to the database.

Your grid is bound to a BindingSource and that is bound to a DataTable in your typed DataSet. That's where you should be making the change. It may be that you should be binding your TextBoxes to the same data, in which case the modifications to the data would happen automatically. If not then you should be copying the data from the TextBoxes back into the typed DataRow you're editing. That grid will automatically reflect that change. YOu then call Update on your table adapter to save those changes from the DataTable back to the database.

In short, don't make changes directly to the database and then try to pull those changes from the database into your local data. Make the changes to the local data first, then save those changes to the database.

Upvotes: 1

Related Questions