Click here to Skip to main content
12,882,820 members (37,240 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB 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

m_dtDelayedDeliveryDays is my datatable.

Please help
Posted 15-Feb-13 8:46am
Updated 15-Feb-13 9:29am
joshrduncan2012 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 15-Feb-13 14:51pm
no i am new to VB so i dont have any clue how to do that...
joshrduncan2012 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 15-Feb-13 14:57pm
its a combo dropdowl list box.
joshrduncan2012 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 15-Feb-13 15:04pm
visual studio 2010...Its a combo box with Drop down list as its DropDownList style...
joshrduncan2012 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
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")

This is from memory but that should be pretty close. You'll have to specify what the ID is.
vidkaat 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 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 15-Feb-13 14:58pm
Nope i am not.
vidkaat 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 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 will help.
vidkaat 15-Feb-13 15:05pm
no my combo has only one value ie...days
ryanb31 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 15-Feb-13 15:07pm
no u r wrong. the design i showed above is my table not the combo box value
ryanb31 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 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 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 15-Feb-13 15:17pm
i dont see any property called Value in the combo box.
ryanb31 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
joshrduncan2012 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 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170422.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2017
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