Results 1 to 5 of 5

Thread: Storing a Key with each item in ComboBox in .NET

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    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

  2. #2
    New Member
    Join Date
    Feb 2003
    Location
    Maputo, Moçambique
    Posts
    13
    Hi Ben!

    One option is to populate the combobox with objects that contain the id (your record key) and your string.

    VB Code:
    1. Public Class myClass
    2.  
    3.     Private m_id As Integer
    4.     Private m_name As String
    5.  
    6.     Public Property Id() As Integer
    7.         Get
    8.             Id = m_id
    9.         End Get
    10.         Set(ByVal Value As Integer)
    11.             m_id = Value
    12.         End Set
    13.     End Property
    14.     Public Property Name() As String
    15.         Get
    16.             Name = m_name
    17.         End Get
    18.         Set(ByVal Value As String)
    19.             m_name = Value
    20.         End Set
    21.     End Property
    22.  
    23.     Public Overrides Function ToString() As String
    24.         Return m_name
    25.     End Function
    26.  
    27. End Class
    And then you can obtain the selected id:
    VB Code:
    1. 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

  3. #3
    Lively Member markmyb's Avatar
    Join Date
    Nov 2001
    Location
    Alberton - South Africa
    Posts
    108
    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."

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297
    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

  5. #5
    Lively Member markmyb's Avatar
    Join Date
    Nov 2001
    Location
    Alberton - South Africa
    Posts
    108
    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
  •  



Click Here to Expand Forum to Full Width