|
-
Oct 8th, 2003, 08:44 AM
#1
Thread Starter
Hyperactive Member
Storing a Key with each item in ComboBox in .NET
Hello Everyone,
I'm using .NET for the first time, and had a question. When I populate my combo box with database values, I was hoping to store the record key with each item so I could then determine which specific record the user chose. I'm trying to avoid binding directly to the database, although why I'm not sure so if you can convince me that's the way to go I will.
Otherwise, I was hoping there would be a tag property or something, but there isn't. Any suggestions?
Thanks!
-Ben
-
Oct 9th, 2003, 10:46 AM
#2
New Member
Hi Ben!
One option is to populate the combobox with objects that contain the id (your record key) and your string.
VB Code:
Public Class myClass
Private m_id As Integer
Private m_name As String
Public Property Id() As Integer
Get
Id = m_id
End Get
Set(ByVal Value As Integer)
m_id = Value
End Set
End Property
Public Property Name() As String
Get
Name = m_name
End Get
Set(ByVal Value As String)
m_name = Value
End Set
End Property
Public Overrides Function ToString() As String
Return m_name
End Function
End Class
And then you can obtain the selected id:
VB Code:
selectedId = CType(myCombobox.SelectedItem, myClass).Id
I started doing like this when began working with .net because I also missed a property for id in the combobox, but maybe there is a better way. If you find one, please let me know!
/Sara
-
Oct 10th, 2003, 03:21 AM
#3
Lively Member
another option it to fill a dataset with all tables you need from the DB.
Eg: on a form you have 3 combo's.
cbo1 (Status)
cbo2 (Type)
cbo3 (Caller)
create an adabter and fill the dataset for each one of these tables. For an example on how to do this. Try using Pirates combo box demo.
http://www.vbforums.com/showthread....threadid=253655
then simply bound the dataset to your combos and walla.
Confusios say "Man who run in front of car get tired."
-
Oct 10th, 2003, 10:06 AM
#4
Thread Starter
Hyperactive Member
Hey Guys,
Thanks for both of your responses.
I ended up keeping an Array alongside of the combo, but I like both of your answers much better. 
I didn't realize I could populate the Combobox with my OWN items (that's awsome) all I have to do is override the ToString method? Don't I need to Inherit a class before I can Override stuff?
On the dataset answer, I don't know anything about a dataset or how they work. That won't leave a connection to my database hanging open, will it?
Thanks again guys!
--Ben
-
Oct 13th, 2003, 01:15 AM
#5
Lively Member
No, datasets do not leave a connection open.
I've closed and set my connection to nothing and am still able to pull data.
Confusios say "Man who run in front of car get tired."
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
|