Populating a ComboBox
Hi!
I used to do web pages with C#, now I 'm doing a windows application,
SinceI can't find dropdown lists I used a combobox and I am trying to populate it from the database
the problem is: I can't load the data, It was supposed to have a textField and a ValueField, how can I do this?
Please any kind of help would be appreciated...
Best Regards
# 1 Re: Populating a ComboBox
You can set up an association to do this (if the ComboBox is of DropDownStyle = DropDownList style).
Have a look at the DataBinding class. Either that or I believe there's a way of doing this from the UI once you've set the DropDownStyle = DropDownList by looking at the "DataBindings" property of the form.
Otherwise you'll have to populate your combo box in code. That's no problem. Have a look at the ComboBox.Items.Add and ComboBox.Items.AddRange methods in MSDN.
Important : If you do this then check for the DesignMode property first and if it is set don't load the values into the combo box, or (my favourite) put the code to load your combo box with values into the OnLoad virtual override of the form.
If you don't do this then the forms designer UI will continually add the values to the combo box every time you call it up - i.e. every time you look at your form you'll see that the combo boxes list grows.
Again, this isn't a problem - just expand the "Windows Forms Designer Generated Code" part in your form's code (double click on it) and do a search on your combo box controls name. After a few "next" presses you'll find an "AddRange" call and this will be huge. Just delete the whole thing and your combo box will be reset back to operating as usual.
Darwen.
# 2 Re: Populating a ComboBox
I dont fully understand what you mean by value field and text field. To my understanding you just define the field as required.
The way I usually populate comoboxes is by creating a class which will store the different values for each entry in the combobox, therefore the object in the combobox will have more than one value.
//to add new items to the combobox
languageBox.Items.Add(new ComboItem("Arabic","lang_ar"));
//the class to store the object info
public ComboItem(String name, String code)
{
m_Name = name;//name to be displayed in the box
m_Code = code;//value that the object represents, this can be changed to any
//other sort of value
//now you override the ToString() function, so in the combobox only the Name will
//be displayed
public override string ToString()
{
return m_Name;
}
//and an output function to retrieve the value of the object
public string GetCode()
{
return m_Code
}
}
Regards
Hope this helps, if so please vote
# 3 Re: Populating a ComboBox
That's a reasonably nice implementation andreshs1 in the fact that you're keeping the string held in the combo box seperate from another linked string.
However for most combo boxes a simple SelectedIndex is sufficient (if it's not sorted).
If you want to go to extremes I have a derived class for ComboBox which detects a Parent change. When its parent changes it hooks into the Load event on the containing form and automatically fills itself via a virtual function.
This makes it truly reusable - i.e. I can just drop this combo box anywhere knowing that it will be filled automatically.
Your implementation is a little heavy handed for my taste : especially when it comes to things like enums which you can quite happily link using a DataBinding object.
If you want custom strings for your enum you need to define a custom TypeConverter and apply it to the enum. Again, usually heavy handed.
SelectedIndex is what I use in 99% of my combo boxes - unless their contents can change dynamically of course.
Darwen.
# 4 Re: Populating a ComboBox
Or, alternatively, a combobox will accept an array of objects by calling
combobox.Items.AddRange(arrayOfObjects);
If you've overriden ToString() this will be displayed in the
combobox.
The nice thing is that you can get a reference to the selected object
by calling
combobox.getSelectedItem() and then casting to your object.
Hope that helps,
Matt