1

I have an Enumeration whitch values I want to aliment to a Combobox.

Enum SortType
  Id
  FirstName
  LastName
End Enum

I vant to associate to that enumeration some strings that I'll be able to set in the combobox. à la "Id", "First Name", "Last Name".

How should I proceed?

For advanced, I, eventually, should be able to internationalize that strings: ("Id", "Nom", "Prénom")

Actually, I have a List(Of String) = new List("Id", "First Name", "Last Name") and associate that list index to a enum value. This is a little embarrassing, because when I need to add or remove some enum values I should re-associate the indexes.

serhio
  • 28,010
  • 62
  • 221
  • 374

2 Answers2

2

In C# I usually use this code:

How to Bind Enum Types to the Dropdown

I think this code will run:

Friend Enum Speed
    Low = 1
    Medium = 2
    High = 3
End Enum

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    DropDownList1.DataSource = System.Enum.GetValues(GetType(Speed))
    DropDownList1.DataBind()


End Sub
danyolgiax
  • 12,798
  • 10
  • 65
  • 116
  • +1. I usually add a little code to put spaces between CamelCase words. (FirstName -> "First Name") – StriplingWarrior Jun 30 '11 at 14:44
  • I intentionally included two words enum names to see that the enum code name and GUI presentation name could differ. Say nothing for internationalization... – serhio Jun 30 '11 at 14:44
1

Take a look at these two questions:

Enum ToString with user friendly strings

How can I internationalize strings representing C# enum values?

I'd go with Jon Skeet's answer and put the string values in a resource file.

Community
  • 1
  • 1
takteek
  • 7,020
  • 2
  • 39
  • 70