Jump to content

Recommended Posts

Posted

Eu tenho um código que serve para visualizar os dados sobre um cliente e nessa mesma form tenho uma picturebox para abrir outra form com a mesma informacao para eliminar, e quando elimino as forms fecham e quando abro a 1ª form o dado que eliminei ainda la aparece, queria que alguem me ajudasse em por exemplo fazer algo do tipo de actualizar...

Form1

Imports System.Data.OleDb
Imports System
Imports System.Windows.Forms
Imports System.Reflection

Public Class Form16

    Private Sub Form16_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim kk As New OleDbConnection(My.Settings.ligacao)

        ' LIGAR A BD
        kk.Open()


        ' ACESSO AOS DADOS
        Dim comandosqll = "SELECT Data, Valor_Glicemia,Tensao_Arterial_Sistolica,Tensao_Arterial_Diastolica  FROM avaliacao where Id_Cliente = " & Form15.ListView1.SelectedItems.Item(0).SubItems(0).Text
        Dim cmdd As New OleDbCommand(comandosqll, kk)

        Dim ff As OleDbDataReader = cmdd.ExecuteReader(CommandBehavior.CloseConnection)


        DataGridView1.RowHeadersVisible = False


        ' CARREGAR OS DADOS
        Dim idx_linha As Integer
        idx_linha = 0

        While ff.Read()
            Dim linha As New DataGridViewRow()
            DataGridView1.Rows.Add(linha)
            DataGridView1.Rows(idx_linha).Cells(0).Value = ff(0).ToString()
            DataGridView1.Rows(idx_linha).Cells(1).Value = ff(1).ToString()
            DataGridView1.Rows(idx_linha).Cells(2).Value = ff(2).ToString()
            DataGridView1.Rows(idx_linha).Cells(3).Value = ff(3).ToString()


            idx_linha = idx_linha + 1


        End While

        ' FECHAR A BD
        ff.Close()
    End Sub

    Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
        Form17.ShowDialog()

    End Sub
End Class

Form2

Imports System.Data.OleDb
Imports System
Imports System.Windows.Forms
Imports System.Reflection

Public Class Form17

    Private Sub Form17_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim kk As New OleDbConnection(My.Settings.ligacao)

        ' LIGAR A BD
        kk.Open()


        ' ACESSO AOS DADOS
        Dim comandosqll = "SELECT Data, Valor_Glicemia,Tensao_Arterial_Sistolica,Tensao_Arterial_Diastolica,id_verificacao  FROM avaliacao where Id_Cliente = " & Form15.ListView1.SelectedItems.Item(0).SubItems(0).Text
        Dim cmdd As New OleDbCommand(comandosqll, kk)

        Dim ff As OleDbDataReader = cmdd.ExecuteReader(CommandBehavior.CloseConnection)


        DataGridView1.RowHeadersVisible = False


        ' CARREGAR OS DADOS
        Dim idx_linha As Integer
        idx_linha = 0

        While ff.Read()
            Dim linhas As New DataGridViewRow()
            DataGridView1.Rows.Add(linhas)
            DataGridView1.Rows(idx_linha).Cells(0).Value = ff(0).ToString()
            DataGridView1.Rows(idx_linha).Cells(1).Value = ff(1).ToString()
            DataGridView1.Rows(idx_linha).Cells(2).Value = ff(2).ToString()
            DataGridView1.Rows(idx_linha).Cells(3).Value = ff(3).ToString()
            DataGridView1.Rows(idx_linha).Cells(4).Value = ff(4).ToString()


            idx_linha = idx_linha + 1


        End While

        ' FECHAR A BD
        ff.Close()
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Try
            Dim msg = MsgBox(" Deseja Eliminar o Cliente?", MsgBoxStyle.YesNo, "Apagar")




            If msg = MsgBoxResult.Yes Then

                Dim lm As String = "delete * from avaliacao  where id_verificacao = " & DataGridView1.Rows(e.RowIndex).Cells(4).Value

                MsgBox("Avaliação eliminada!", MsgBoxStyle.Information, "Apagado")



                Me.Close()
                Form16.Close()

                ' Inicia uma ligação à bse de dados
                Using connection As New OleDbConnection(My.Settings.ligacao)

                    ' Define o comando e os parâmetros

                    Dim command As New OleDbCommand(lm, connection)


                    ' Abre a ligação e insere o registo
                    connection.Open()
                    Dim x As Integer = command.ExecuteNonQuery()
                    If x <> 1 Then
                        Throw New ArgumentException("Não foi possível inserir o registo na base de dados!")
                        'Else
                    End If


                End Using

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub
End Class
Posted

E estás a apagar de facto da base de dados alguma coisa? verifica como está a ir a instrução SQL, mete um break nesta linha para veres as variáveis: Dim command As New OleDbCommand(lm, connection)

Knowledge to the masses


Posted

Solução 1:

Ao carregar no botao Apagar mandas carregar a datagrid outra vez,ou seja, o mesmo que tens no load da form...

Solução 2: (bah)

Assim que apaga, fechas o form e mandas abrir outra vez...

a 2 é muita ruim... =/

Já o fiz e não faz nada! Nao sei porqe  😛

Posted

E estás a apagar de facto da base de dados alguma coisa? verifica como está a ir a instrução SQL, mete um break nesta linha para veres as variáveis: Dim command As New OleDbCommand(lm, connection)

Sim apaga! Porque se eu fechar o programa todo e o voltar a abrir, ja apagou os dados

Posted

Mas ele não tem a DataGridView com datasource nenhum...

Experimenta assim (este ainda só actualiza a 2ª From (se correr tudo bem))

Imports System.Data.OleDb

Imports System

Imports System.Windows.Forms

Imports System.Reflection

Public Class Form17

Private Sub carrega_dados()

Dim kk As New OleDbConnection(My.Settings.ligacao)

      ' LIGAR A BD

        kk.Open()

        ' ACESSO AOS DADOS

        Dim comandosqll = "SELECT Data, Valor_Glicemia,Tensao_Arterial_Sistolica,Tensao_Arterial_Diastolica,id_verificacao  FROM avaliacao where Id_Cliente = " & Form15.ListView1.SelectedItems.Item(0).SubItems(0).Text

        Dim cmdd As New OleDbCommand(comandosqll, kk)

        Dim ff As OleDbDataReader = cmdd.ExecuteReader(CommandBehavior.CloseConnection)

DataGridView1.Rows.Clear()

        DataGridView1.RowHeadersVisible = False

      ' CARREGAR OS DADOS

        Dim idx_linha As Integer

        idx_linha = 0

        While ff.Read()

            Dim linhas As New DataGridViewRow()

            DataGridView1.Rows.Add(linhas)

            DataGridView1.Rows(idx_linha).Cells(0).Value = ff(0).ToString()

            DataGridView1.Rows(idx_linha).Cells(1).Value = ff(1).ToString()

            DataGridView1.Rows(idx_linha).Cells(2).Value = ff(2).ToString()

            DataGridView1.Rows(idx_linha).Cells(3).Value = ff(3).ToString()

            DataGridView1.Rows(idx_linha).Cells(4).Value = ff(4).ToString()

            idx_linha = idx_linha + 1

        End While

        ' FECHAR A BD

        ff.Close()

End Sub

    Private Sub Form17_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        carrega_dados()

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

        Try

            Dim msg = MsgBox(" Deseja Eliminar o Cliente?", MsgBoxStyle.YesNo, "Apagar")

            If msg = MsgBoxResult.Yes Then

                Dim lm As String = "delete * from avaliacao  where id_verificacao = " & DataGridView1.Rows(e.RowIndex).Cells(4).Value

                MsgBox("Avaliação eliminada!", MsgBoxStyle.Information, "Apagado")

              ' Inicia uma ligação à bse de dados

                Using connection As New OleDbConnection(My.Settings.ligacao)

                    ' Define o comando e os parâmetros

                    Dim command As New OleDbCommand(lm, connection)

                    ' Abre a ligação e insere o registo

                    connection.Open()

                    Dim x As Integer = command.ExecuteNonQuery()

                    If x <> 1 Then

                        Throw New ArgumentException("Não foi possível inserir o registo na base de dados!")

                        'Else

                    End If

                End Using

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

carrega_dados()

    End Sub

End Class

Knowledge to the masses


Posted

Mas ele não tem a DataGridView com datasource nenhum...

Experimenta assim (este ainda só actualiza a 2ª From (se correr tudo bem))

Imports System.Data.OleDb

Imports System

Imports System.Windows.Forms

Imports System.Reflection

Public Class Form17

Private Sub carrega_dados()

Dim kk As New OleDbConnection(My.Settings.ligacao)

      ' LIGAR A BD

        kk.Open()

        ' ACESSO AOS DADOS

        Dim comandosqll = "SELECT Data, Valor_Glicemia,Tensao_Arterial_Sistolica,Tensao_Arterial_Diastolica,id_verificacao  FROM avaliacao where Id_Cliente = " & Form15.ListView1.SelectedItems.Item(0).SubItems(0).Text

        Dim cmdd As New OleDbCommand(comandosqll, kk)

        Dim ff As OleDbDataReader = cmdd.ExecuteReader(CommandBehavior.CloseConnection)

DataGridView1.Rows.Clear()

        DataGridView1.RowHeadersVisible = False

      ' CARREGAR OS DADOS

        Dim idx_linha As Integer

        idx_linha = 0

        While ff.Read()

            Dim linhas As New DataGridViewRow()

            DataGridView1.Rows.Add(linhas)

            DataGridView1.Rows(idx_linha).Cells(0).Value = ff(0).ToString()

            DataGridView1.Rows(idx_linha).Cells(1).Value = ff(1).ToString()

            DataGridView1.Rows(idx_linha).Cells(2).Value = ff(2).ToString()

            DataGridView1.Rows(idx_linha).Cells(3).Value = ff(3).ToString()

            DataGridView1.Rows(idx_linha).Cells(4).Value = ff(4).ToString()

            idx_linha = idx_linha + 1

        End While

        ' FECHAR A BD

        ff.Close()

End Sub

    Private Sub Form17_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        carrega_dados()

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

        Try

            Dim msg = MsgBox(" Deseja Eliminar o Cliente?", MsgBoxStyle.YesNo, "Apagar")

            If msg = MsgBoxResult.Yes Then

                Dim lm As String = "delete * from avaliacao  where id_verificacao = " & DataGridView1.Rows(e.RowIndex).Cells(4).Value

                MsgBox("Avaliação eliminada!", MsgBoxStyle.Information, "Apagado")

              ' Inicia uma ligação à bse de dados

                Using connection As New OleDbConnection(My.Settings.ligacao)

                    ' Define o comando e os parâmetros

                    Dim command As New OleDbCommand(lm, connection)

                    ' Abre a ligação e insere o registo

                    connection.Open()

                    Dim x As Integer = command.ExecuteNonQuery()

                    If x <> 1 Then

                        Throw New ArgumentException("Não foi possível inserir o registo na base de dados!")

                        'Else

                    End If

                End Using

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

carrega_dados()

    End Sub

End Class

Funciona perfeitamente  😛 Obrigado

E agora para a 1ª form?

Posted

Para a 1ª form

Imports System.Data.OleDb

Imports System

Imports System.Windows.Forms

Imports System.Reflection

Public Class Form16

Private Sub carrega_dados()

' LIGAR A BD

        kk.Open()

        ' ACESSO AOS DADOS

        Dim comandosqll = "SELECT Data, Valor_Glicemia,Tensao_Arterial_Sistolica,Tensao_Arterial_Diastolica  FROM avaliacao where Id_Cliente = " & Form15.ListView1.SelectedItems.Item(0).SubItems(0).Text

        Dim cmdd As New OleDbCommand(comandosqll, kk)

        Dim ff As OleDbDataReader = cmdd.ExecuteReader(CommandBehavior.CloseConnection)

        DataGridView1.Rows.Clear()

        DataGridView1.RowHeadersVisible = False

      ' CARREGAR OS DADOS

        Dim idx_linha As Integer

        idx_linha = 0

        While ff.Read()

            Dim linha As New DataGridViewRow()

            DataGridView1.Rows.Add(linha)

            DataGridView1.Rows(idx_linha).Cells(0).Value = ff(0).ToString()

            DataGridView1.Rows(idx_linha).Cells(1).Value = ff(1).ToString()

            DataGridView1.Rows(idx_linha).Cells(2).Value = ff(2).ToString()

            DataGridView1.Rows(idx_linha).Cells(3).Value = ff(3).ToString()

            idx_linha = idx_linha + 1

        End While

      ' FECHAR A BD

        ff.Close()

End Sub

    Private Sub Form16_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim kk As New OleDbConnection(My.Settings.ligacao)

carrega_dados()

    End Sub

    Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click

        Form17.ShowDialog()

    End Sub

End Class

Na Form17 terás de Acrescentar isto depois do "carrega_dados()" lá no fundo.

Form16.carrega_dados()

Knowledge to the masses


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.