I want to retrive data from table into combobox ..when the user click combobox items it's corresponding data will be show.

In details :

I have PERSON table , my program allow to the user search by : Person Name
When the user enters the name into TextBox the combobox items will be all
names existed in database same which user entered.

when the user selects item from combobox a new form will show all person' data

here what I tried

Me.PersonComboBox.DataSource = tblPerson ' tblPerson = DataSet.Tables("PERSON")
Me.PersonComboBox.DisplayMember="FirstName"
Me.PersonComboBox.ValueMember="PersonID"  ' PersonID is tblPerson PK

but that code shows all names , It does not allow to (Select .... Where Name = ... )

Also tried this:

For Each dr In tblPerson.Select("FirstName ='" & Me.nameTextBox.Text & "' ")
Me.PersonComboBox.Items.Add(dr("FirstName").ToString & " " & dr("LastName").ToString)
Me.PersonComboBox.ValueMember = dr("PersonID")
Next

but Combobox (SelectedIndexChanged) event always show just last item data


Thanks alot for your help
:)

Recommended Answers

All 5 Replies

Use DataView object:

Dim dv as DataView=tblPerson.DefaultView
dv.RowFilter = "Name like '" & nameTextBox.Text & "%'"

Me.PersonComboBox.DataSource = dv
Me.PersonComboBox.DisplayMember="FirstName"
Me.PersonComboBox.ValueMember="PersonID"

Thanks alot for you adatapost
Really I did not think about using data view ..
I will try it .. then I will tell you what happen with my problem
Thanks very much

Hi adatapost :)
I tried your way (data view) but it did not scuess :(
and how can be when user selects item its related details(reset record columns)are displayed in a new form or msgBox ??? !!! :-/

Thanks for help

Handler the SelectedIndexChanged event of PersonComboBox.

Private Sub PersonComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PersonComboBox.SelectedIndexChanged
        Dim row As DataRowView = CType(PersonComboBox.SelectedItem, DataRowView)
        If Not IsNothing(row) Then
            MsgBox(row("MiddleName") & "  " & row("FirstName"))

        End If
    End Sub

Hi adatapost :)
I think I have problem with dataview it did not scuess..
but I found another way : :idea: and I it scuessed

Public ar As New ArrayList ' to save Person ID
For Each CurrentPersonRecord In tblPerson.Select("LastName ='" & Me.LastnameTextBox.Text & "' "and FirststName ='" & Me.FirstnameTextBox.Text & "' " and MiddleName ='" & Me.MiddlenameTextBox.Text & "' ")

Me.ComboBox1.Items.Add(CurrentPersonRecord("FirstName").ToString & " " & CurrentPersonRecord("MiddleName").ToString)
'/// save Person ID into array to match arry elements index with combobox index
ar.Add(CurrentPersonRecord("PersonID").ToString)
Next

Combobox handler :

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim id As Integer = ar.Item(ComboBox1.SelectedIndex) '// match Person ID with SelectedIndex
CurrentPersonRecord = tblSpecialist.Select("PersonID='" & id & "' ").ElementAt(0)
DisplayPersonInfo.NameLabel.Text = CurrentPersonRecord("FirstName").ToString & " " & CurrentPersonRecord("MiddleName").ToString & " " & CurrentPersonRecord("LastName").ToString
            DisplayPersonInfo.DisplayNationalIDLabel.Text = CurrentPersonRecord("NationalID").ToString
            DisplayPersonInfo.DisplayGenderLabel.Text = CurrentPersonRecord("Gender").ToString
            DisplayPersonInfo.DisplayTelNumberLabel.Text = CurrentPersonRecord("TelNumber").ToString
            DisplayPersonInfo.DisplayPersonIDLabel.Text = CurrentPersonRecord("PersonID").ToString
            DisplayPersonInfo.DisplayDateOfBirthLabel.Text = CurrentPersonRecord("DateOfBirth").ToString

DisplayPersonInfo.show()
     
    End Sub

Thank you adatapost :) my problem is solved ..But I still think your solution is more optimal .. :icon_cool:

Good Luck


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.