Visual Basic.NET - Actualizar DataGridView Automaticamente

 
Vista:
sin imagen de perfil

Actualizar DataGridView Automaticamente

Publicado por Dany (5 intervenciones) el 14/04/2014 22:28:45
Que tal a todos espero me puedan ayudar.

En mi formulario WindowsForm tengo 4cajas de texto y 5 botones de comando los botones son: Buscar,Agregar,Guardar,Editar y Eliminar.
Basicamente los botones hacen la funcion descrita y funcionan correctamente mostrandome en cada caja de texto el registro seleccionado. En el mismo formulario tengo un DGV en este cargo los datos de la tabla MySql donde se hacen las consultas lo que quiero es que si Agrego,Edito o Elimino un registro Automaticamente se actualice el DGV si busco un registro se filtre el registro alguine que me pueda hechar la mano por favor.
Esto es algo que tengo de codigo:

Para la conexion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Imports MySql.Data.MySqlClient
 
Module Modulo_conexion
    Public cadena As String
    Public conexion As New MySqlConnection
    Public sql As String
    Public mycommand As New MySqlCommand
    Public myAdapter As New MySqlDataAdapter
 
 
 
    Public Sub conectarme()
 
        'realizo la conexion'
        Try
            'localhost
            cadena = "server='localhost'; user id=root; pwd=root; database=rutas_de_cable"
            conexion.ConnectionString = cadena
            conexion.Open()
            MsgBox("conexion establecida!!!")
 
        Catch ex As Exception
            MsgBox("No es Posible Establecer Conexion con la Base de Datos Debido a Problemas de Configuracion, Verifique los Parametros de Conexion Por Favor!", MsgBoxStyle.Critical, "Advertencia")
        End Try
 
    End Sub
 
End Module

En el boton buscar:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
encontro = 0
        Dim nombre, ruta, turno As String
 
        nombre = ""
        ruta = ""
        turno = ""
 
        If conexion.State = ConnectionState.Closed Then
            conexion.Open()
        End If
 
        If String.IsNullOrEmpty(TxtNoGafete.Text) Then
            MsgBox("El campo 'Empleado' no puede ser vacio")
            TxtNoGafete.Focus()
            Exit Sub
        End If
 
        mycommand = New MySqlCommand("select *from asignacion_de_rutas where Empleado='" & Me.TxtNoGafete.Text & "'", conexion)
        mycommand.Connection = conexion
        Dim drcampo As MySqlDataReader
        drcampo = mycommand.ExecuteReader()
 
        Try
            Do While drcampo.Read
                encontro = 1
                nombre = drcampo.GetString(2)
                ruta = drcampo.GetString(3)
                turno = drcampo.GetString(4)
 
            Loop
        Catch ex As Exception
            MsgBox("Ha ocurrido un Error!", MsgBoxStyle.Critical, "Error")
        End Try
        conexion.Close()
 
        If (encontro = 1) Then
            Me.TxtNombre.Text = nombre
            Me.TxtRuta.Text = ruta
            Me.TxtTurno.Text = turno
            Me.DGVAsig.DataSource = drcampo
            MsgBox("Usuario encontrado!", MsgBoxStyle.Information, "Notificacion")
 
        Else
 
            MsgBox("el usuario que usted esta buscando no se encuentra en la base de datos, verifique!", MsgBoxStyle.Exclamation, "Notificacion")
            Me.TxtNoGafete.Focus()
        End If
    End Sub
 
    Private Sub CmdGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdGuardar.Click
 
        validar()
 
    End Sub
 
 encontro = 0
        If conexion.State = ConnectionState.Closed Then
            conexion.Open()
        End If
 
        mycommand = New MySqlCommand("select *from asignacion_de_rutas where Empleado='" & Me.TxtNoGafete.Text & "'", conexion)
        mycommand.Connection = conexion
        Dim drcampo As MySqlDataReader
        drcampo = mycommand.ExecuteReader()
 
        Try
            Do While drcampo.Read
                encontro = 1
 
            Loop
        Catch ex As Exception
            MsgBox("Ha ocurrido un Error!", MsgBoxStyle.Critical, "Error")
        End Try
        conexion.Close()
 
        If (encontro = 1) Then
            MsgBox("No se puede Guardar, ya existe ese empleado!", MsgBoxStyle.Exclamation, "Notificacion")
        Else
            guardar()
        End If
 
    End Sub

Agradezco su ayuda.

Saludos.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder