Hi all...
I need help in Combo Box, List box and Enums in Windows Applications in C#.
My requirement is -
I have an Enum - Colours.
public enum Colours
{
Red=1, Blue, Green, Purple, Brown, Yellow , Black}
I want to bind this enum with combo box in such a way that-
Combo Box items are - Red, Blue, Green, Purple, Brown, Yellow , Black.
Combo Box Value Member - Index of colours i.e. 1,2,3,5,6,7.
Listbox items are - Red, Blue, Green, Purple, Brown, Yellow , Black.
ListBox selectedValue -Index of colours i.e. 1,2,3,5,6,7.
----------
Scenario-When user selects a colour(say Blue), then value (here 2) should be stored in database.
Please help..
Thanks all in advance
Loading
VulpesPosted Jun 15, 2013, 6:35 AM
comboBox1.DataSource = Enum.GetValues (typeof(Colours));
The names of the colours will then be displayed in the box and the SelectedValue (and SelectedItem) properties, both of which return an object, will then contain a boxed version of the index of the selected colour.
To retrieve the index as an int you will therefore need to use a cast or apply a conversion function:
Riddhi ValechaPosted Jun 15, 2013, 12:41 AM
Thanks dear...
But, I have to use enums...
Do you have any solution for this ??
Riyaz AkhtarPosted Jun 14, 2013, 9:37 AM
You have to create a class to achieve this, see the below sample:
Class:
Assign the value:
Bind this object to combobox or listbox.
Thanks,
RA