将Dictionary(Of Integer,String)绑定到DropDownList的DataTextField属性

时间:2010-10-29 19:44:27

标签: asp.net vb.net collections

我有一个像这样定义的State类:

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

名称(x)包含不同语言的州名。

我从StateManager.GetAllStates()方法中获取State的集合 我想将此集合绑定到DropDownList。问题是 我找不到如何设置DataTextField属性让我们说stateList.Name(1) 这是我所在州的英文名称。

Dim stateList As StateCollection = StateManager.GetAllStates() 

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

有人有想法吗?

由于

2 个答案:

答案 0 :(得分:3)

使用数据源时,不能使用复杂表达式来指定值。

创建一个源代码,您可以在其中获取所需的值:

Me.DataSource = stateList.Select( _
  Function(s as State) New With { .Id = s.StateId, .Name = s.Name(1) } _
);
Me.DataValueField = "Id"
Me.DataTextField = "Name"

答案 1 :(得分:0)

而不是“Name(1).Value”,你试过了吗?

Me.DataTextField = name(1).ToString
相关问题