473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3500
> 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.Select edValue 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****@discuss ions.microsoft. com> wrote in message
news:27******** *************** ***********@mic rosoft.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(stri ng) on
the way back.

Cheers,

Greg
"Nathan" <Na****@discuss ions.microsoft. com> wrote in message
news:27******** *************** ***********@mic rosoft.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()
{
InitializeCompo nent();

comboBox1.DataS ource = 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
12628
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 value="red"/> <xsd:enumeration value="yellow"/> <xsd:enumeration value="blue"/> </xsd:simpleType>
1
2037
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 problem is this: the schema contains an element called "TRENDED" and it has an attribute called "type" whose value can be "previous" or "current". This element can occur twice in the XML, once with the attribute-value "previous" and a second time with...
13
3059
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: to catch unhandled cases, but of course this is only at run-time. Example: public enum MyEnum { one, two, three, four }
4
5658
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" for a second if you don't mind). However, it existed within intuitive language semantics, whereas the new "yield" keyword, while highly convenient, is also one of the most gross warping of language concepts to date... public IEnumerator...
2
1727
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 binding, so if I have an array with class level scope and I am operating on that array at procedure level I fabricate a "dummy array" and copy the class level array into it. Then after performing the task I can copy the "dummy array" back into the...
0
3531
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: Parameters = new object; Parameters = "fileName.mht"; Parameters = WdSaveFormat.wdFormatWebArchive; Parameters = false;
0
2330
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 through internet explorer when hitting the ..asmx url, i cannot generate a proxy class to use in my winforms assembly. When i try to generate a proxy, i get no error message but nor do i get a reference.vb so there is no type info. If i then try...
3
2541
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"> <xs:enumeration value="Mon"/> <xs:enumeration value="Tue"/> <xs:enumeration value="Wed"/>
7
4453
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
3621
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 way to databind like when using a table as list backing store? Thanks, Charlie
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.