passado Posted December 29, 2009 at 02:40 PM Report Share #302968 Posted December 29, 2009 at 02:40 PM 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 More sharing options...
passado Posted January 5, 2010 at 09:02 PM Author Report Share #304335 Posted January 5, 2010 at 09:02 PM 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now