views:

16

answers:

2

I have a State class defined like this:

Public Class State
    Public Property StateId As Integer
    Public Property Name As Dictionary(Of Integer, String)
End Class

Name(x) contains the state name in different languages.

I get a collection of State from the method StateManager.GetAllStates() and I want to bind this collection to a DropDownList. The problem is that I can't find how to set the DataTextField property to let's say stateList.Name(1) which is the english name of my state.

Dim stateList As StateCollection = StateManager.GetAllStates() 

Me.DataSource = stateList
Me.DataValueField = "StateId"
Me.DataTextField = "Name(1).Value" <-- Problem here
Me.DataBind()

Anyone have an idea?

Thanks

A: 

Rather than "Name(1).Value", have you tried:

Me.DataTextField = name(1).ToString
ewitkows
Same problem, DataBinding: 'State' does not contain a property with the name 'Name(1)'.
Jason
Maybe cast the property as a local Dictionary variable, and then pull it? Dim myNameDictionary = State.Name Me.DataTextField = myNameDictionary (1).ToString
ewitkows
@ewitkows: Note that the DataTextField is a string naming the property to use from the data source, not the value itself.
Guffa
@Guffa: nice catch, sorry about that!
ewitkows
+2  A: 

When you use a data source, you can't use complex expressions to specify values.

Create a source where you extact the values that you need:

Me.DataSource = stateList.Select( _
  Function(s as State) New With { .Id = s.StateId, .Name = s.Name(1) } _
);
Me.DataValueField = "Id"
Me.DataTextField = "Name"
Guffa
Works perfectly. Thank you very much!
Jason