|
-
Apr 12th, 2008, 12:38 AM
#1
Thread Starter
New Member
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.
-
Apr 12th, 2008, 12:48 AM
#2
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.
-
Apr 12th, 2008, 12:53 AM
#3
Thread Starter
New Member
Re: help with combo box
 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?
-
Apr 12th, 2008, 01:22 AM
#4
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:
If haciendaCombo.SelectedItem IsNot Nothing Dim haciendaID As Integer = CInt(haciendaCombo.SelectedValue) End If
-
Apr 12th, 2008, 01:35 AM
#5
Thread Starter
New Member
Re: help with combo box
how do i create a data relation i dont know how to do it, can u please help.
-
Apr 12th, 2008, 01:57 AM
#6
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.
-
Apr 12th, 2008, 01:59 AM
#7
Thread Starter
New Member
Re: help with combo box
i already got it. thanx for your help.
-
Apr 12th, 2008, 03:20 AM
#8
Thread Starter
New Member
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 .
-
Apr 12th, 2008, 03:43 AM
#9
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|