473,326 Members | 2,255 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,326 software developers and data experts.

How can I use an enum as the datasource for a combobox?

I need a datasource for a third party combobox and the data should come from
an enum.

I have figured out how to use the enum to create a 2-dimensioned array
imitating the form of Key, Value such as a DictionaryEntry object would like.
But how can I now transform the array into DictionaryEntry object?

Or is there a better way to feed an enum to a ComboBox.Datasource?

I'm using .NET framework 1.1 and VStudio.

Thanks
Nov 16 '05 #1
7 11351
Hi Nina,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to convert each object in the
array to an DictionaryEntry. If there is any misunderstanding, please feel
free to let me know.

As far as I know, there isn't a way to do this directly. We have to go
through each object and create a new DictionaryEntry object according to
the key and value. Here's an example:

DictionaryEntry de = new DictionaryEntry(object key, object value);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #2
Thanks Kevin. I can *almost* see how to do it. But please give me a little
more help. I'm trying to generalize the following code:

this.categoryCombobox.DataSource = new DictionaryEntry[]
{
new DictionaryEntry(ContactCategories.Commissionaire,
ContactCategories.Commissionaire.ToString()),

new DictionaryEntry(ContactCategories.Contract,
ContactCategories.Contract.ToString()),

new DictionaryEntry(ContactCategories.Finance,
ContactCategories.Finance.ToString()),

new DictionaryEntry(ContactCategories.Marketing,
ContactCategories.Marketing.ToString()),

new DictionaryEntry(ContactCategories.Ops, ContactCategories.Ops.ToString()),

new DictionaryEntry(ContactCategories.Other,
ContactCategories.Other.ToString())

};

I know how to iterate through the enum and get the Key/Value pairs, but how
to plug this into the DictionaryEntry object?

Nina
"Kevin Yu [MSFT]" wrote:
Hi Nina,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to convert each object in the
array to an DictionaryEntry. If there is any misunderstanding, please feel
free to let me know.

As far as I know, there isn't a way to do this directly. We have to go
through each object and create a new DictionaryEntry object according to
the key and value. Here's an example:

DictionaryEntry de = new DictionaryEntry(object key, object value);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #3
Hi Nina,

Do you mean that you cannot make the DictionaryEntry display with their
values? I think your code is right. But we also have to set the
DisplayMember and ValueMember of the comboBox.

DictionaryEntry[] de = new DictionaryEntry[]{new
DictionaryEntry("a","b"), new DictionaryEntry("c","d")};
this.comboBox1.DataSource = de;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #4
Hi Kevin,
Thanks. My problem is, how do I load the DictionaryEntry objects with the
data from the enum. Here's some code, which I know won't work ...

foreach(string availabilityCode in Enum.GetNames(typeof(AvailabilityCode)))
{
DictionaryEntry[] de = new DictionaryEntry[] {new DictionaryEntry(i,
availabilityCode)};
i ++;
}

Thank you for your help Kevin.
Nina
"Kevin Yu [MSFT]" wrote:
Hi Nina,

Do you mean that you cannot make the DictionaryEntry display with their
values? I think your code is right. But we also have to set the
DisplayMember and ValueMember of the comboBox.

DictionaryEntry[] de = new DictionaryEntry[]{new
DictionaryEntry("a","b"), new DictionaryEntry("c","d")};
this.comboBox1.DataSource = de;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #5
Hi Nina,

Do you mean that you need to display the enumeration names in the combobox
and assign values for them from 0,1,2.....? If so, here is an example

string[] names = Enum.GetNames(typeof(AvailabilityCode));
DictionaryEntry[] de = new DictionaryEntry[names.Length];
for(int i=0;i<names.Length;i++)
de[i] = new DictionaryEntry(i, names[i]);
this.comboBox1.DataSource = de;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #6
Kevin - I owe you a beer. Please let me know if you ever come to Vancouver,
Canada.
Nina

"Kevin Yu [MSFT]" wrote:
Hi Nina,

Do you mean that you need to display the enumeration names in the combobox
and assign values for them from 0,1,2.....? If so, here is an example

string[] names = Enum.GetNames(typeof(AvailabilityCode));
DictionaryEntry[] de = new DictionaryEntry[names.Length];
for(int i=0;i<names.Length;i++)
de[i] = new DictionaryEntry(i, names[i]);
this.comboBox1.DataSource = de;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #7
You're welcome, Nina. Thanks for sharing your experience with all the
people here. If you have any questions, please feel free to post them in
the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #8

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

Similar topics

3
by: Tamir Khason | last post by:
Why this does not work??? cb1 - combobox DataProvider.DataProvider.DS_BringRacks() - DataSet cb1.DataSource=DataProvider.DataProvider.DS_BringRacks().Tables; cb1.DisplayMember = "id"; ...
2
by: jobi | last post by:
Hi, I have this combobox which has a table from a dataset as datasource. When I click an item on the combobox-list , I would like to display (in textfield) the value from an other column that...
3
by: Franky | last post by:
does anybody know how to use all values of an enumeration in a combobox? i have an enum type defined like public enum EnumTest { val1 = 1, val2 = 2, val4 = 4, }
1
by: Brien King | last post by:
I am binding to a collection of Business Objects in a GridView (Windows App, ..NET 2.0). In my Business Object I have a Property that is an ENUM. I want a Drop down box in the Grid that is...
1
by: Brian | last post by:
Is there a way to bind a nullable Enum property to a combo box? When choosing a value, and then leaving the combo box, the value reverts back to the original value. It seems the databinding...
1
by: progressdll | last post by:
I'm trying to change the behaviour of setting the DataSource property of combobox. Here is the behaviour I now see. Setting the DataSource property of combobox to an ArrayList will visual...
2
by: =?Utf-8?B?UmljaGFyZCBUb2NjaQ==?= | last post by:
I'm learning Visual Basic using the .Net Framework 2.0. I have been playing with consuming web services in applications. I am using a free web service that gives me information for currency...
1
by: Gerrit | last post by:
Hello, I have an enum, example: enum MyEnum { My_Value_1, My_Value_2 }
2
by: samueltilden | last post by:
This problem should be simple. I am using Visual Studio 2003, Version 1.1 I am writing a desktop application in which I am binding a DataTable to the DataSource of a ComboBox. // I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.