开发者

Binding a Dictionary(Of Integer, String) to the DataTextField property of a DropDownList

开发者 https://www.devze.com 2023-01-22 09:17 出处:网络
I have a State class defined like this: Public Class State Public Property StateId As Integer Public Property Name As Dictionary(Of Integer, String)

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 st开发者_StackOverflow中文版ateList.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


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"


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

Me.DataTextField = name(1).ToString
0

精彩评论

暂无评论...
验证码 换一张
取 消