1,105,352 Community Members

Binding table record into combobox

Member Avatar
Sara Tech
Newbie Poster
17 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
:)

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

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"
Member Avatar
Sara Tech
Newbie Poster
17 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Sara Tech
Newbie Poster
17 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

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
Member Avatar
Sara Tech
Newbie Poster
17 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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


Question Answered as of 3 Years Ago by __avd
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article