Print
Add To Favorites
Email To Friend
Rate This Article
|
CodeSnip: How to Add Data/Values for Corresponding Entries in a Combo Box Using Visual Basic .NET
|
Published:
06 Nov 2006
|
Abstract
While using Visual Basic .NET many developers have experienced a drawback in maintaining data or value for corresponding entries in a combo box. In this article Sandeep provides a solution for the issue with the help of code samples. |
 |
by Sandeep Acharya
Feedback
|
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days):
27932/
299
|
|
|
|
| Introduction |
Most of the .NET developers used to experience a common
problem while working with combo boxes. Generally, in a combo box or dropdown
we used to store a value-text pair. For example, suppose we have an employee
Table in a Database where we have a unique Employee ID for each employee. It
is obvious that we will want to access the records by referring to the Employee
ID's.
Now, assume a scenario where we need to display the list of
employees in a Dropdown or combo box. Displaying the list is not a big deal,
but there is a twist in keeping the ID's against corresponding Employee Names
in the Dropdown. There is no direct method available in .NET to achieve this
functionality.
Below is a code snippet that successfully implements the
functionality in a twisted manner.
|
|
|
|
|
Article Feedback
User Comments
Title:
Excellent Sandeep
Name:
Rahul Gupta
Date:
1/9/2008 7:16:47 AM
Comment:
Done good work, it really solved the problem, but mention it because of override of toString function
Thanking you Rahul Gupta Software Developer www.maestros.net
|
Title:
anal programmer
Name:
paris hilton
Date:
6/2/2007 8:12:40 AM
Comment:
TOPS!!great article.Thank you.
|
Title:
Web Application Developer
Name:
Liby George
Date:
5/13/2007 1:13:09 PM
Comment:
Very Nice and it is working fine. Thanks...
Liby George
|
Title:
Want some Exercise???
Name:
Neetu
Date:
4/23/2007 5:01:39 AM
Comment:
It's Really nice and very helpful stuff . But I request you to give me the only exercise for C#.net I want to practice by applying the logics on the projects. Please can you help me out of this??? If you have wny questions kindly revert me back on my email id as adwani_neetu@yahoo.co.in Also Can I get totally C# Concepts/tutorial on internet. If yes then send me the link to my email id.
Thanks & Regards, Neetu
|
Title:
Select a combo item by Value
Name:
Jus
Date:
4/23/2007 1:30:51 AM
Comment:
Ahh... Sandeep, just spotted previous entry. Thought I would have to do that.
|
Title:
Select a combo item by Value
Name:
Jus
Date:
4/23/2007 1:25:35 AM
Comment:
Ok - so how would you set the currently selected item in the list by it's Value. Normally I would use cbo.SelectedValue = xxx to set the current item.
|
Title:
Cool
Name:
wackoyacky@yahoo.com.ph
Date:
2/3/2007 7:50:57 PM
Comment:
Cool work around! Very helpful especially for webby guys who's used with asp.net dropdownlist like me! :)
|
Title:
Very Good
Name:
Raef
Date:
1/26/2007 4:17:26 AM
Comment:
Thank you very much .. this is really a hot topic i keep searching for this code long time ago
|
Title:
An issue.. Please help!
Name:
Kathir
Date:
1/17/2007 2:59:07 AM
Comment:
Hi Sandeep, It's a great tip! Sandeep - I have an issue. I'm using VS.NET 2003 Enterprise Architect and i'm not getting the property DATA of ComboBox1.SelectedItem. Is this the problem withe version and edition i'm using? Thanks.
|
Title:
For Karunya
Name:
Sandeep
Date:
1/5/2007 11:43:10 AM
Comment:
Hello Karunya, Try something like this : *********************************************************
For Each oDataRow In oDataTable.Rows cmbEmployee.Items.Add(New DataDescription( _ oDataRow.Item("employeeId"), _ oDataRow.Item("employeeName"))) If CInt(oDataRow.Item("employeeId")) = 6 Then cmbEmployee.SelectedIndex = cmbEmployee.Items.Count - 1 End If Next ******************************************************** The above lines of code sets the SelectedIndex at the time of populating the dropdown.
You can also take another loop to cover the entire dropdown items and then inside that check for the appropriate dropdown.data.
I guess you may solve your problem by using any of them. But I suggest the first one as the second one involves an extra loop.
please feel free to ask any doubts if you have.
Thanks
Sandeep
|
Title:
Setting the Selected Index using the data value
Name:
Karunya
Date:
1/5/2007 11:22:08 AM
Comment:
This solution has fixed the text/value issues. Thanx.
I have a question. I am trying to set the selected index of the combo box, with the data(value).
Something like 'cbxEmployee.SelectedItem.Data = empCode'
I would appreciate any kindof help.
Thanx
|
Title:
No the event will not get fired for each entry in the collection.....
Name:
Vikram.
Date:
12/9/2006 11:53:33 PM
Comment:
Please do verify this yourself.. I have verified this... the selectedindexchanged event is fired only once for the item displayed in the combox box!!!!!
|
Title:
nice
Name:
K.senthilkumar
Date:
11/24/2006 6:13:48 AM
Comment:
thank u,,i know about how to full both text and value in ASP.net but i dont know in VB.net but with the help of u, i have cleared my doubts....
|
Title:
good for beginner
Name:
Sandeep
Date:
11/13/2006 5:44:17 AM
Comment:
this is very useful to the beginners and also to me
|
Title:
Some other options, things to consider
Name:
Christopher May
Date:
11/10/2006 12:44:18 PM
Comment:
I wrote up some comments and other options on my blog:
http://www.chrismay.org/2006/11/10/Dealing+With+WinForms+Combobox+And+Namevalue+Items.aspx
|
Title:
Thanks, Nice post on the Confusion
Name:
Sumit Kumar Dhal
Date:
11/7/2006 2:14:03 AM
Comment:
Nice job Sandeep, This, will help most to achive the solution. More, this article made me understand, how I can utilize my own classes/objects in a more meaningful way, to optimize the .net control usage.
But, i have a question, not to you, but all microsoft people, why they do not have name-value property combination, in to the combo item type. Can I conclude that there is a lot more scope to make .net more developer friendly :)
sumit
|
Title:
Nice and informative.
Name:
Sarit
Date:
11/6/2006 1:34:16 AM
Comment:
This sol will solve many problem dealing with text/value issues.
|
|
Product Spotlight
|
|
|