combobox hades!

riversr

Member
Joined
Oct 30, 2006
Messages
12
Programming Experience
3-5
I'm sure I'm making this more difficult than it is but I'm stumped. I want to populate a combobox with two items from a database table. The table has a name and an associated id. I want to display the name in the combobox dropdown and then use the associated id in the code. I've tried everyway I can think of to make this work but nothing seems to get me there. I created a simple object that contains the name and ID and then inserted that into the Items of the combobox, but then at run time I see the object in the dropdown intead of the name. This is logically what I want to do:

combobox1.items.add(new listitem("Jones", 123))

This is how I would do it in ASP.net code, but of course that doesn't work in VB for a windows application.

What am I missing?

Thanks,

rivers
 
Retrieve the data into a DataTable then bind the data to the control:
VB.NET:
myComboBox.DisplayMember = "Name"
myComboBox.ValueMember = "ID"
myComboBox.DataSource = myDataTable
The ID for the selected Name will then be returned by the SelectedValue property.

There's no way to do it without data-binding. If you weren't going to bind you'd just have to declare your own class, with the two values exposed via properties and the ToString method overridden to return the value you wanted displayed. The SelectedValue would then return the selected object and you'd have to cast it and get the appropriate property value. You could, of course, derive your own ComboBox class and add the appropriate functionality.
 
Is there an alternative?

Thanks for the response. Maybe I'm trying to use the wrong component. Is there a simple dropdown similar to ASP.net that I can accomplish this with? If there is I certainly haven't found it.

P.S. -- I had tried the binding to a table method but that didn't allow me to have a blank entry at the top, e.g. a none selection. Maybe there is some way to insert a blank row in the table before I bind it to the combobox.

Thanks again,

rivers
 
The ComboBox is the equivalent control to the ASP.NET DropDownList. It's a different contyrol so it works differently, but it fills the same role. If you want custom behaviour then inherit the class and implement it, which is the whole point of OOP. Also, you don't need a blank entry at the top because you just have to set the SelectedItem to Nothing or the SelectedIndex to -1 to clear the selection.
 
Thanks

Thanks for the help, that solved my problem. I would still like to write a custom control but I ran into some problems with getting it to load correctly. If you have any thoughts on this I would appreciate it.

I wanted to build a customer combobox that is automatically populated with a name and id from a database(as described earlier), but I want to be able to provide some information to the custom control that determines which info is loaded, e.g. specify the SQL select command that is used to build the table that populates the combobox. I only know how to populate the combobox at load time, not later. Is there a way to dynamically provide the select string to the control and then have it re-load?

Thanks again...
 
That is not the job of the control so you should not incorporate it into the control. Data access and UI should be kept spearate and have a well-defined interface. Such a system already exists in the BindingSource class. Retrieve your data into a DataTable and bind it to a BindingSource, then bind that to your ComboBox. Now you simply set the Filter property of the BindingSource and the bound control will only display matching records.
 
Thanks for the help, that solved my problem. I would still like to write a custom control but I ran into some problems with getting it to load correctly. If you have any thoughts on this I would appreciate it.

I wanted to build a customer combobox that is automatically populated with a name and id from a database(as described earlier), but I want to be able to provide some information to the custom control that determines which info is loaded, e.g. specify the SQL select command that is used to build the table that populates the combobox. I only know how to populate the combobox at load time, not later. Is there a way to dynamically provide the select string to the control and then have it re-load?

Thanks again...

Take a read of the DW2 link in my signature, there is a section about creating lookup tables and using them. Also "Creating a FOrm to Search Data" shows you how to make a search sql that is parameterized. If you change your setup so it only fills the records you want into the combo, you can avoid the Filter technique

Which you pick (FillByXXX or Filter) depends on the dataset size. If youre downlaoding 10,000 rows and then using a Filter, it is much more preferable to use FillByXXX (db does searching). FOr a small number of items, use Filter as per jmc' post
 
Back
Top