Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have the following code which populates the dropdown lists from the database. This works fine. Now, when the user selects a particular days from the list, I want to get the Days Id of the selected employee. i.e. I need to get the Dayid value. I can do this if the Dayid is loaded in the dropdown list, but in this case only the days are displayed. Please let me know how I can get the Dayid value even though it is not loaded in the dropdown list.
 
This is how i load the listbox from the database:
For Each dr As DataRow In m_dtDelayedDeliveryDays.Rows
            Me.cmbDelayedDeliveryDays.Items.Add(dr("DeliveryDelayDays"))
        Next
m_dtDelayedDeliveryDays is my datatable.
 
Please help
Posted 15-Feb-13 8:46am
vidkaat528
Edited 15-Feb-13 9:29am
v3
Comments
joshrduncan2012 at 15-Feb-13 14:50pm
   
Are you referring to the position of the element in the combobox (i.e. first position, second position, third, etc?)
vidkaat at 15-Feb-13 14:51pm
   
no i am new to VB so i dont have any clue how to do that...
joshrduncan2012 at 15-Feb-13 14:55pm
   
First of all, your header is a little misleading. You mention comboboxes in your header but your question refers to listboxes. Make sure that is clear.
 
I strongly urge you to research listbox and their associated properties. There is a command provided that will return the position of the element in the listbox.
vidkaat at 15-Feb-13 14:57pm
   
its a combo dropdowl list box.
joshrduncan2012 at 15-Feb-13 14:59pm
   
What version of Visual Studio are you using? I've never heard of a listbox having drop down capabilities.
vidkaat at 15-Feb-13 15:04pm
   
visual studio 2010...Its a combo box with Drop down list as its DropDownList style...
joshrduncan2012 at 15-Feb-13 15:11pm
   
That's just known as a "combobox". It is not known as a "combo list box". Comboboxes have the list feature.
 
I go back to my earlier comment that you need to research comboboxes and their associated commands. There is a command provided by the combobox control that will return the integer position of the element within the combobox.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You actually need to add the ListItem separately, instead of in the constructor. For example:
 
For Each dr As DataRow In m_dtDelayedDeliveryDays.Rows
   Dim tempList as New ListItem
   tempList.Text = dr("DeliveryDelayDays")
   tempList.Value = dr("DeliveryDelayDaysID")
   Me.cmbDelayedDeliveryDays.Items.Add(tempList)
Next
 
This is from memory but that should be pretty close. You'll have to specify what the ID is.
  Permalink  
Comments
vidkaat at 15-Feb-13 14:55pm
   
I dont want to add the id to the drop down list i want the corresponding id of the selected day from the drop down list.
ryanb31 at 15-Feb-13 14:57pm
   
Correct. You need to put the ID into the ListItem value property and what you want the user to see in the ListItem.Text property. Are you familiar with the dropdown's having both a value and a text?
vidkaat at 15-Feb-13 14:58pm
   
Nope i am not.
ryanb31 at 15-Feb-13 15:04pm
   
So, a drop down item has 2 values, the actual text you see and then there is a value behind the scenes that is in html but that the user does not see. This is a perfect example of when you use it. You show the user the Day and then you get the actual value. Maybe looking at http://www.w3schools.com/tags/tag_select.asp will help.
vidkaat at 15-Feb-13 15:05pm
   
no my combo has only one value ie...days
vidkaat at 15-Feb-13 15:03pm
   
What i have done so far is I loaded the combo listbox value from the database for which the code is above. Now what i would like to do is based on the selection made from the combo dropdown list i need to pull the corresponding id..For ex:Mt table looks like this
6 0
8 3
9 7
10 15
11 20
12 30
So now if the user selectd 3 from the combo listbox i shud get the corresponding id as 8.I need logic to do this.
ryanb31 at 15-Feb-13 15:05pm
   
So, you actually have 2 ids? Is that what you are saying. You have 3 values you need on each list item? The text, and then 2 ids?
vidkaat at 15-Feb-13 15:07pm
   
no u r wrong. the design i showed above is my table not the combo box value
ryanb31 at 15-Feb-13 15:09pm
   
OK, so where is the confusion? A listitem can have a value and a text. Set the text to be what the user will see and set the value to be the value you actually need.
vidkaat at 15-Feb-13 15:13pm
   
My question is i need the logic to get the Dayid based on the Days selected from the combo box.
ryanb31 at 15-Feb-13 15:14pm
   
When you are adding the items to the combo, you need to set the Dayid in the ListItems value property.
vidkaat at 15-Feb-13 15:17pm
   
i dont see any property called Value in the combo box.
ryanb31 at 15-Feb-13 15:30pm
   
I was mistaking this with ASP.Net. Apparently, windows forms combobox does not have a separate item class so you have to create your own class and then can do it. For some simple examples, see http://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source
joshrduncan2012 at 15-Feb-13 16:34pm
   
Ryan, I think he's referring to the SelectedIndex property of the Windows Forms comboboxes. Won't that return an integer value representing the position of the element in the combobox item listing?
ryanb31 at 15-Feb-13 16:35pm
   
It will. But OP keeps asking for how to get the ID, so that won't give ID. OP says they can get the ID if that's what the put in the list but they need to display days and then get ID after.
joshrduncan2012 at 15-Feb-13 17:02pm
   
I think he should enter each element in as a string and use the Split method to pick apart each part of the element.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 686
1 OriginalGriff 385
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,158
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100