Combobox First Name Surname - Select First name to Populate Surname fileld-VBForums
Results 1 to 4 of 4

Thread: Combobox First Name Surname - Select First name to Populate Surname fileld

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    11

    Combobox First Name Surname - Select First name to Populate Surname fileld

    I have a SQL table with firstname and Surname, I want to select the first name and the surname populates automatically.

    I have used Data Bound Items.

    Data Binding Mode and tried to change the Data Source display and Value on the Surname Combobox but the data messes up.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    509

    Re: Combobox First Name Surname - Select First name to Populate Surname fileld

    Explain a bit clearer. I might help you, but don't have a clue what you're saying.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    11

    Re: Combobox First Name Surname - Select First name to Populate Surname fileld

    I have a sql table with 2 colums first name and surname.

    I have a form with 2 comboboxes First Name and Surname

    I want to select First Name from the 1st Combobox and the 2nd combox populates with the surname

  4. #4
    Hyperactive Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    509

    Re: Combobox First Name Surname - Select First name to Populate Surname fileld

    that is easy, just tell me, is second combobox without datasource ?....anyway, you could do this, example:

    1. when binding 1st combobox you select both columns - you can do different binding as this, important is only that you set NAME as DisplayMember:
    Code:
    Dim Done_Loading as Boolean
    
     Private Sub Combobox_Enter(sender As Object, e As EventArgs) Handles Combobox1.Enter
    
            Dim Combo As ComboBox = DirectCast(sender, ComboBox)
           
            If Combo.Items.Count > 0 Then
                Exit Sub
    
            Else 
      
              DoneLoading = False
              Dim dtb As New DataTable()
    
                Try
                  
                    Using cmd As New OracleCommand("SELECT Name, Surname From Mytable", conn)
    
                      
                        Using dad As New OracleDataAdapter()
                            dad.SelectCommand = cmd
                            dad.Fill(dtb)
                        End Using
                       
                        Combo.DisplayMember = "Name"
                        Combo.DataSource = dtb
    
                    End Using
    
                Catch ex As Exception
                    MessageBox.Show("Error: " & ex.Message)
    
                End Try
    
              
                Combo.SelectedIndex = -1
    
               
                Done_Loading = True
    
            End If
    
        End Sub
    2. Then in same Combobox_Text changed event you use LINQ:

    Code:
         'pressuming your names in table are row(0)
            If Done_Loading = True Then
               
              If Combobox1.Items.Cast(Of DataRowView)().Any(Function(drv) drv.Row(0).ToString().Equals(Combobox1.Text, StringComparison.CurrentCultureIgnoreCase)) Then
    	        If Not (String.IsNullOrEmpty(Combobox1.Text)) Then
    		
    		Dim drv As DataRowView = DirectCast(Combobox1.Items(Combobox1.FindString(Combobox1.Text)), DataRowView)
    		Combobox2.Text = drv.Row("Surname").ToString()
    	  End If
            End If

    This will change text in your second combobox, after you will add datasource in Combobox1. Not tested though, I just translated It from C#, but It should work.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.