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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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."
 
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
 
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."
 
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
 
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 = new DictionaryEntry(i, names);
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."
 
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."
 
Back
Top