Binding a list to an enumeration...

C

Charlie@CBFC

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
 
N

Nicholas Paldino [.NET/C# MVP]

Charlie,

Generally, you could cycle through the enumeration and add the items
manually.

If you wanted to take advantage of data binding, then I would create a
generic class like so:

public class EnumListItem<T>
{
public readonly T Value;
public readonly string Display;

public EnumListItem(T value, string display)
{
// Assign the values.
this.Value = value;
this.Display = display;
}
}

Then, you can have a method which will return an IList<EnumListItem>
implementation which you can bind your listbox to. Of course, you set your
DisplayMember to "Display" and your ValueMember to "Value".

Hope this helps.
 
C

Charlie@CBFC

Interesting. Thanks!
Nicholas Paldino said:
Charlie,

Generally, you could cycle through the enumeration and add the items
manually.

If you wanted to take advantage of data binding, then I would create a
generic class like so:

public class EnumListItem<T>
{
public readonly T Value;
public readonly string Display;

public EnumListItem(T value, string display)
{
// Assign the values.
this.Value = value;
this.Display = display;
}
}

Then, you can have a method which will return an IList<EnumListItem>
implementation which you can bind your listbox to. Of course, you set
your DisplayMember to "Display" and your ValueMember to "Value".

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Charlie@CBFC said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top