473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Binding to an enumeration

I'd like to use an enumeration as a datasource for a drop-down box. Is there
a way to do this?
May 1 '06 #1
5 3483
> I'd like to use an enumeration as a datasource for a drop-down box. Is there
a way to do this?


I don't think you can use straight databinding, however:
look into the Enum.GetNames() and Enum.GetValues() methods (and maybe
Enum.Parse() ). With these you can get the names and values in you enum
or translate a string into the enum value.
Hans Kesting
May 1 '06 #2
I'm assuming that's because a databinding datasource must implement IList?
Then I guess the best way to do it would be to build a generic class that
generates an IList for any enum. I'll see if I can do something along those
lines. Thanks!

"Hans Kesting" wrote:
I'd like to use an enumeration as a datasource for a drop-down box. Is there
a way to do this?


I don't think you can use straight databinding, however:
look into the Enum.GetNames() and Enum.GetValues() methods (and maybe
Enum.Parse() ). With these you can get the names and values in you enum
or translate a string into the enum value.
Hans Kesting

May 1 '06 #3
Nathan,
As Han suggests:

Have you tried something like:

public enum SomeEnum
{
Value1 = 1,
Value2 = 2
}

Array list = Enum.GetValues(typeof(SomeEnum));

Then bind the above list variable?

Some people bind to the names, via Enum.GetNames, as opposed to the
values. I prefer using the values as above in Windows Forms as the
ComboBox.SelectedValue is the enum value rather then a string... In Web
Forms I don't see an advantage to using the values, but then again I have
not bound to enums in web forms...
Seeing as Enum.GetValues returns an Array, and Array implements IList you
can simply bind to the array that Enum.GetValues returns!
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Nathan" <Na****@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
| I'd like to use an enumeration as a datasource for a drop-down box. Is
there
| a way to do this?
May 1 '06 #4
You should be able to use Enum.GetNames .. then use Enum.Parse(string) on
the way back.

Cheers,

Greg
"Nathan" <Na****@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
I'd like to use an enumeration as a datasource for a drop-down box. Is
there
a way to do this?

May 1 '06 #5
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

comboBox1.DataSource = Enum.GetValues(typeof(MyColors));

}
public enum MyColors
{
Red,
White,
Blue,
Purple,
Green
}
}
May 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
1
by: Andy Wells | last post by:
I'm using VB.NET and I have an application that binds a schema to the main form's controls, and the user has the ability to load an XML file through the schema and into the bound form. My...
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
2
by: mark | last post by:
I understand that writing programs with option strict on is the best way to obtain stable applications. I have also found the applications to run much faster. Option strict on disallows late...
0
by: mancha28 | last post by:
Hi, I have create Word object by late binding to open a Word document and save it under another name. This new saved document should be saved as mht-file. Following code I impelemented: ...
0
by: Steven Bolard | last post by:
Hello, I am trying to port my .net 1.1 application to 2.0. I am using vs2005. I am trying to get my webservices to run and although i can compile them and and get wsdl and service descriptions...
3
by: Davidoff | last post by:
Hi, I parse an XML file with a XSD schema. One XmlNode has an attribute whose type is a restriction of xs:string : <xs:simpleType name="stypeDay"> <xs:restriction base="xs:string">...
7
by: Nathan | last post by:
I'd like to use an enumeration as a datasource for a drop-down box. Is there a way to do this?
2
by: Charlie | last post by:
Hi: How would I bind a list to an enumeration so that enumeration value becomes list values and enumeration constants becomes list text? Do you loop through enum and add manually or is there a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.