locked
Populating Listbox with DataReader RRS feed

  • Question

  •  

    Hi,

    I was wondering if someone can help me.

    Im usinig VB.NET, I populate a listbox with values from a database. It works when I populate in with the unique ID field, which is a (autonumber), and then convert it to a text-field from one of the other columns, because the user will find it easier to understand what "Installation of aircon" means, than what the number "22" means.

    But when I use the Listbox.SelectedItem function, I actually need the ID-value(22) for my next select statement, can someone please assists me in how I can go about.

    Please thanks

    SJB

    Monday, February 27, 2006 12:01 PM

Answers

  • Instead of adding only the 'name', you'll need to add more information to the ListBox.

    Luckely, the Items collection of the ListBox accepts 'objects'.   This means that you can add an instance of a custom class.

    For instance, you can create a class that holds the name and its corresponding Id:

    <code>

    public class MyListItems

    {

        private int id;

        private string description;

     

        public int Id { get .. .set ... }

        public string description { get .. set ... }

     

        public override string ToString()

        {

              return description;

        }

    }</code>

    You can create instances of this class; for each record in your DB, create an object and populate it with the correct values.   Then, add those objects to your listbox.

    Monday, February 27, 2006 1:10 PM