Jump to content
Sign in to follow this  
carlosmorais

Update a DataGridView

Recommended Posts

carlosmorais

Boas, tenho uma form, para filtrar os registos, em que o campo SAIDA, está vazio.

para eu executar o botão para salvar teria de declarar o DataSet e o Adapter, mas dá erro no "Dim Adapter As New MySqlDataAdapter(command)" : Value of type 'String' cannot be converted to 'MySql.Data.MySqlClient.MySqlCommand'.

nota: se declarar dentro do evento  Button1_click, preenche a DataGrid sem problema...

estava a seguir isto: http://vbtuga.blogspot.com/2008/04/vbnet-utilizando-o-controlo.html


                Dim DataSet As New DataSet
                Dim Adapter As New MySqlDataAdapter(command)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                 Dim connectionString As String = "Server=localhost;" & _
                  "Database=picaoponto;" & _
                  "Uid=root;" & _
                  "Pwd=123pt;" & _
                  "Connect Timeout=30;"
                Dim connection As New MySqlConnection(connectionString)
                Dim query As String = "SELECT codfuncionario, data, entrada, saida FROM registodeponto WHERE saida IS NULL"
                Dim command As New MySqlCommand(query, connection)
                'Dim DataSet As New DataSet
                'Dim Adapter As New MySqlDataAdapter(command)
                Adapter.Fill(DataSet, "registodeponto")
                DataGridView1.DataSource = DataSet.Tables("registodeponto")
end sub

...
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cb As New MySqlCommandBuilder(Adapter)

        Adapter.Update(DataSet, "registodeponto")
    End Sub

Share this post


Link to post
Share on other sites
obamaself

Tenta assim:

Private objCon As New SqlConnection
objcon = my.settings.conn.. 'Aqui para teres esta connecção tens de adicionar a base de dados . My Project / Settings  e escolhes o type connection string  , vais a coluna "value" e é so seguir o que diz
Dim sSql = "select..."

        Dim sqlcomando As New SqlClient.SqlCommand(sSql, objCon)
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable("metodo_fisico")
        da.SelectCommand = sqlcomando
        Me.abrir()
        da.Fill(dt)
        Me.fechar()
        Return dt

Share this post


Link to post
Share on other sites
carlosmorais

@obamaself, não consigo fazer nada com isso, não percebo o código... (já adicionei a conexão ás settings)

@bioshock, eu consigo fazer INSERT's, UPDATES... mas o que eu quero é editar os dados da DataGrid e posteriormente fazer o UPDATE..

http://img825.imageshack.us/img825/7575/saidas.png

depois de serem listados os registo sem saida, eu queria editar os campos de Saida e depois salvar.

Cumps.

Share this post


Link to post
Share on other sites
obamaself

para isso tens de fazer o update de todos.

For linha as integer =0 to dg.row.count-1

dim funcionario as funcionario ' Estructura

For coluna as integer=0 to dg.columns.count-1
funcionario.cod_functionario=dg.item("cod_funcionario",linha).value
funcionario.data=dg.item("data",linha).value
'ETC ETC

next
'Aqui metes o comando sql
next

Share this post


Link to post
Share on other sites
carlosmorais

já consigo... esse código ajudou bastante... faltam só uns retoques, mas já testei e funciona.  :confused:

fica aqui a resolução:

For linha As Integer = 0 To DataGridView1.RowCount - 1
            For coluna As Integer = 0 To DataGridView1.Columns.Count - 1
                Dim myConnectionString As String
                Dim connection As New MySqlConnection
                myConnectionString = "Server=localhost;" & _
                          "Database=picaoponto;" & _
                          "Uid=root;" & _
                          "Pwd=123pt;" & _
                          "Connect Timeout=30;"
                connection.ConnectionString = myConnectionString
                Dim MySQL As String = " UPDATE registodeponto SET saida = @saida WHERE codfuncionario = @codfunc AND entrada = @entrada AND data = @data AND saida IS NULL "
                Dim command As New MySqlCommand(MySQL, connection)
                command.Parameters.Add("@codfunc", MySqlDbType.VarChar).Value = DataGridView1.Item("codfuncionario", linha).Value
                command.Parameters.Add("@entrada", MySqlDbType.VarChar).Value = DataGridView1.Item("entrada", linha).Value
                command.Parameters.Add("@data", MySqlDbType.VarChar).Value = DataGridView1.Item("data", linha).Value
                command.Parameters.Add("@saida", MySqlDbType.VarChar).Value = DataGridView1.Item("saida", linha).Value

                If connection.State <> ConnectionState.Open Then
                    connection.Open()
                End If
                Dim reader As MySqlDataReader = command.ExecuteReader()
                connection.Close()
                connection = Nothing
                command = Nothing
            Next
        Next
        MsgBox("Dados alterados com sucesso")
        DataGridView1.DataSource = Nothing
        Me.DataGridView1.Rows.Clear()

Obrigado.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×
×
  • 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.