Jump to content

[Resolvido]Introduzir valor em coluna de datagridview


passado
 Share

Recommended Posts

Ola pessoal, eu tenho uma gridview para inserir varios contactos a um determinado cliente:

                List_ContactosTableAdapter.Update(BDDataSet.List_Contactos)
                BDDataSet.List_Contactos.Clear()

isto é o que uso para actualizar os dados, mas neste gridview tenho uma coluna que é o id do cliente a que pertence o contacto, a minha questão é como posso eu colocar o id sem ter que o escrever, eu ir buscar o id sei so nao sei como o inserir sem que o utilizador de por isso

Link to comment
Share on other sites

Aqui fica a solução

Private Sub DataGridViewContactos_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridViewContactos.UserAddedRow
        If BT_addcliente.Text = "Guardar" Then
            Dim ultimoid As Integer
            Try
                Dim myConnectionString As String = My.Settings.BDConnectionString
                Dim SQL As String = "SELECT MAX(id_cliente) FROM TB_Clientes"
                Dim connection As New SqlConnection(myConnectionString)
                Dim command As New SqlCommand(SQL, connection)
                connection.Open()
                ultimoid = command.ExecuteScalar
                DataGridViewContactos.CurrentRow.Cells(1).Value = ultimoid + 1
                connection.Close()
                connection = Nothing
                command = Nothing
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        ElseIf BT_editcliente.Text = "Guardar" Then
            DataGridViewContactos.CurrentRow.Cells(1).Value = TextBoxID.Text
        End If
    End Sub

Se detectarem alguma falha ou uma forma melhor avisem 😉

Link to comment
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
 Share

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