Results 1 to 9 of 9

Thread: help with combo box

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    13

    help with combo box

    VB 2005
    im using two combo box and i bind the two

    (cb_dailyplanter and cb_dailyhacienda)
    -cb_dailyplanter-contains the list planter name
    -cb_dailyhacienda-contains the list of hacienda name

    whenever i change the cb_dailyplanter the data of the cb_dailyhacienda changes, it depends which planter owns the hacienda

    ex.

    planter ------------hacienda
    henry--------------Ahacienda
    -----------------Bhacienda
    my problem is i want to get the primary key of hacienda not the hacienda_name (Ahacienda,Bhacienda)

    this is the table

    hac_num -------hac_name
    1-----------------Ahacienda
    2------------------Bhacienda

    how to get the primary key of the table if the hac_name(hacienda name is displayed on the combo box)
    Last edited by syntax101; Apr 12th, 2008 at 12:44 AM.

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: help with combo box

    i dont think you can read it from the combo box. you will probably have to open a connection and iterate through the items.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    13

    Re: help with combo box

    Quote Originally Posted by Lord Orwell
    i dont think you can read it from the combo box. you will probably have to open a connection and iterate through the items.
    how do iterate it. can you give me a hint how to do it?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with combo box

    If you've bound the data correctly then it should be simple.

    1. Get your data into two DataTables in the same DataSet.
    2. Create a DataRelation between them.
    3. Bind one BindingSource to the DataSet with the parent table name as the DataMember.
    4. Bind a second BindingSource to the first BindingSource with the relation name as the DataMember.
    5. Bind the parent ComboBox to the first BindingSource, setting the ValueMember to the name of the primary key column.
    6. Bind the child ComboBox to the second BindingSource, setting the ValueMember to the name of the primary key column.

    Now the filtering of the child data will occur automatically. When the user selects a child record its primary key value is available via the SelectedValue property of the second ComboBox, e.g.
    vb.net Code:
    1. If haciendaCombo.SelectedItem IsNot Nothing
    2.     Dim haciendaID As Integer = CInt(haciendaCombo.SelectedValue)
    3. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    13

    Re: help with combo box

    how do i create a data relation i dont know how to do it, can u please help.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with combo box

    Funny you should ask that. I've been considering for some time submitting some code to the CodeBank to demonstrate this type of thing. Prompted by this thread I just did it. You can find it here. See the GetDataSet method in the posted code for the addition of a DataRelation to the DataSet. There are various ways to do it. They are all very similar and that is probably the easiest.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    13

    Re: help with combo box

    i already got it. thanx for your help.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    13

    Re: help with combo box

    sorry, it still wont work, the data that i retrieve is the selectedindex of the datacombo. im using the designer mode im not coding it .

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with combo box

    Then don't retrieve the SelectedIndex. Retrieve the SelectedValue. It makes no difference whether you set up your data-binding in the designer or you write the code yourself. All the designer does is generate the code for you, so it's all done with code in the end either way. If you have set the ValueMember property of the ComboBox to the name of the column containing the bound table's primary key, when the user selects an item the primary key value of the corresponding row will be returned by the ComboBox's SelectedValue property. That's what I said in post #4 and it hasn't changed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width