2012-08-08 5 views
2

내가 여기, 드롭 다운 값을 정렬 시도하고 내 코드드롭 다운 목록에서 알파벳순으로 값을 정렬하는 방법은 무엇입니까? vb.net

For Each Keystring as Long in HashValue.Keys 
     Dim LItem As New ListItem 
     LItem.Text = cw.Name.ToString() 
     LItem.Value = Keystring.ToString 
     ddRole.Items.Add(LItem) 
    Next 

내가 LItem.Sort을 시도합니다. 하지만 정렬 속성이 정의되지 않았습니다.

값을 정렬하는 가장 좋은 방법을 알려주세요. 감사합니다

+1

추가하기 전에 목록을 정렬하십시오. – Oded

답변

1

빠른 방법은 ListItemList에 넣는 것입니다. 그런 다음 기본 List .sort() 메소드를 사용하여 해당 목록을 정렬하십시오. 그런 다음 드롭 다운 메뉴에 바인딩하십시오.

dim ddList as List(Of ListItem) 
For Each Keystring as Long in HashValue.Keys 
    Dim LItem As New ListItem 
    LItem.Text = cw.Name.ToString() 
    LItem.Value = Keystring.ToString 
    ddList.add(LItem) 
Next 

ddList.Sort() 
ddRole.DataSource = ddList 
ddRole.DataBind() 
+0

'For Each Keystring For HashValue.Keys'이 (가) 사용 된 이유는 무엇입니까, 이해할 수 없습니다. 이전 게시물이지만 문제가 있습니다. 하하하 –

2

OrderBy을 사용해 보셨습니까?

For Each Keystring as Long in HashValue.Keys.OrderBy(Function(key) key) 
     Dim LItem As New ListItem 
     LItem.Text = cw.Name.ToString() 
     LItem.Value = Keystring.ToString 
     ddRole.Items.Add(LItem) 
Next 
1

람다와 시도 : 물론

Items2Sort.Sort(function(x1,x2) x1.CompareTo(x2)) 

을 항목이 문자열 인 경우.

관련 문제