Bind Enum to Drop Down List?

Posts   
 
    
tlyle
User
Posts: 8
Joined: 14-Jan-2005
# Posted on: 19-May-2005 17:04:56   

I have a typed list, and I would like populate a drop down list at runtime with the Fields in the typed list. I would use this to dynamically build a predicate factory.

Is there a way to bind the drop down list to an enumerator? Or is there some other way to get the field list to in a bindable format?

I am using VB.NET with the Self Servicing code.

Thanks for your help.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-May-2005 11:42:29   

You can bind a typed list to a dropdownlist, just set the datavalue and datatext properties (or whatever they're called on the control you're using) to the right fields in the typed list.

Frans Bouma | Lead developer LLBLGen Pro
tlyle
User
Posts: 8
Joined: 14-Jan-2005
# Posted on: 20-May-2005 17:36:02   

Sorry if I didn't make myself clear. I am not wanting to bind the typed list to the dropdown. I am wanting to populate a dropdown with a list of the field names. For example the generated enum:

    ''' <summary>
    ''' Index Enum To fast-access TypedList Fields In the Columns collection of the Typed List: ProspectList
    ''' Auto-generated, Do Not modify.
    ''' </summary>
    Public Enum ProspectListTypedListFieldIndex
        ProspectID
        FirstName
        MiddleName
        LastName
        City
        State
        RecontactDate
        DateLastCommunication
        Recruiter
        AmountOfFields
    End Enum

I want to use this dropdown to build the predicate factory to limit rows returned in my typed list. I tried using: DropDownList1.DataSource = ProspectListTypedListFieldIndex but "'ProspectListTypedListFieldIndex' is a type and cannot be used as an expression."

The code I finally came up with is:

 Dim v As Integer
           Dim i As ListItem
           For Each v In [Enum].GetValues(GetType(ProspectListTypedListFieldIndex))
            i = New ListItem
            i.Value = v.ToString
            i.Text = [Enum].GetName(GetType(ProspectListTypedListFieldIndex), v)
                DropDownList1.Items.Add(i)
            Next

Is there an easier way?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-May-2005 18:03:34   

Dim i = 0
Dim item As ListItem
For i = 0 to ProspectListTypedListFieldIndex.AmountOfFields-1
    item = New ListItem()
    i.Value = i.ToString()
    i.Text = CType(i, ProspectListTypedListFieldIndex).ToString()
Next i

I'm not sure if this works, in C# it does, but I don't know if ToString on an enum value returns the name of the enum value in VB.NET or that it simply returns the integer value.

Frans Bouma | Lead developer LLBLGen Pro
tlyle
User
Posts: 8
Joined: 14-Jan-2005
# Posted on: 20-May-2005 20:16:11   

Thanks, I'll give that a try.

tlyle
User
Posts: 8
Joined: 14-Jan-2005
# Posted on: 25-May-2005 14:55:13   

Otis wrote:

I'm not sure if this works, in C# it does, but I don't know if ToString on an enum value returns the name of the enum value in VB.NET or that it simply returns the integer value.

It worked great, and in VB it returns the name.

Thanks again.

jtgooding
User
Posts: 126
Joined: 26-Apr-2004
# Posted on: 25-May-2005 15:12:30   

I do this all the time I use C# but this should translate well into VB:

To populate I do this:

comboBox1.Items.AddRange(Enum.GetNames(typeof(ExpenseItemStateEnum)));

This adds all the string equvilants of the enum to the combobox.

Then in the selected item changed event I put:

ExpenseItemStateEnum StatusFilter = (ExpenseItemStateEnum)Enum.Parse(typeof(ExpenseItemStateEnum), dxcboStatusFilter.SelectedItem.ToString(), true);

This will convert the text back to the enum value.

Hope that helps, I like one line solutions hehe.

John