Tips and tricks for .NET using ASP and VB code.

Alphabetize drop down list and remove duplicate items.

    Protected Sub AlphabetizeDropDownListAndRemoveDuplicates(ThisDropDownList As DropDownList)
Dim AlphabetizedItems As New SortedList
For Each ThisListItem As ListItem In ThisDropDownList.Items
If AlphabetizedItems.Contains(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Trim(ThisListItem.Text))) = False Then
AlphabetizedItems.Add(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Trim(ThisListItem.Text)), ThisListItem.Value)
End If
Next ThisListItem
ThisDropDownList.Items.Clear()
For Each AlphabetizedItem As DictionaryEntry In AlphabetizedItems
ThisDropDownList.Items.Add(New ListItem(AlphabetizedItem.Key, AlphabetizedItem.Value))
Next AlphabetizedItem
End Sub

No comments:

Post a Comment

Search This Blog