Click here to Skip to main content
Click here to Skip to main content

Sort Method for an ASP.NET DropDownList

By , 28 Sep 2003
 

Introduction

I recently found a requirement in my ASP.NET application that needed to be able to sort a DropDownList control. Oddly enough I couldn't find a sort method within the framework, so I made my own. The code below shows what I did as a quick option, but you can of course use similar code to create a custom control that inherits from the DropDownList control.

Code

    Private Sub SortDropDown(ByVal dd As DropDownList)
        Dim ar As ListItem()
        Dim i As Long = 0
        For Each li As ListItem In dd.Items
            ReDim Preserve ar(i)
            ar(i) = li
            i += 1
        Next
        Dim ar1 As Array = ar

        ar1.Sort(ar1, New ListItemComparer)
        dd.Items.Clear()
        dd.Items.AddRange(ar1)
    End Sub
    Private Class ListItemComparer _
        Implements IComparer
        
        Public Function Compare(ByVal x As Object, _
              ByVal y As Object) As Integer _
              Implements System.Collections.IComparer.Compare
            Dim a As ListItem = x
            Dim b As ListItem = y
            Dim c As New CaseInsensitiveComparer
            Return c.Compare(a.Text, b.Text)
        End Function
    End Class

Summary

As I mentioned, this can easily be put into a custom control by simply creating a new class. Inherit from the DropDownList control and add the above code to the class. That's all. When you create your ASP.NET web form, you can drag and drop a normal DropDownList to your form, and edit the declaration in your code behind form, to use your custom class. Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Domenic
Web Developer
United States United States
I've been writing software applications since the early 90's from ASP and VB5 to C# and ASP.NET. I am currently working on law enforcement and criminal justice applications delivered over the web. I've always been astounded by the fact that the only 2 industries that refer to customers as "users" are narcotics and software development. Wink | ;-)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralDropDown List Selected Index Change Problemmemberjahirhstu29-Aug-09 4:49 
GeneralPreserve VALUE while sortingmemberprouhiaw@guerrillamailblock.com30-Jul-09 0:18 
GeneralThanks!memberdsilk3-Jul-08 10:52 
GeneralErrormemberhalliej2x1-Aug-06 21:45 
GeneralRe: ErrormemberDomenic2-Aug-06 2:27 
GeneralRe: Errormemberhalliej2x2-Aug-06 19:16 
GeneralRe: ErrormemberDomenic3-Aug-06 9:41 
QuestionRe: ErrormemberGregorio Cybill11-Aug-08 17:58 
GeneralRe: ErrormemberMohammed Aziz Elnahrawi5-Oct-06 20:31 
GeneralShortcut using CopyTo method on prealloced arraymemberhafthor27-Feb-06 5:22 
GeneralRe: Shortcut using CopyTo method on prealloced arraymemberLouvaras4-Feb-09 3:43 
GeneralGood code, but let's make it even bettermemberHamidTheProgrammer9-Aug-05 10:34 
GeneralRe: Good code, but let's make it even bettermemberDomenic10-Aug-05 2:08 
GeneralWould just like to say...sussAnonymous4-Aug-05 0:51 
GeneralRe: Would just like to say...memberDomenic4-Aug-05 2:18 
GeneralSyntax errormemberchatelainj1-Sep-04 8:41 
GeneralRe: Syntax errorsussAnonymous3-May-05 8:21 
Generalc# version wrapped in a SortableDropDownListmemberSteinar Herland2-Apr-04 22:57 
GeneralRe: c# version wrapped in a SortableDropDownListmemberPaul Rogan10-Jan-06 0:32 
GeneralRe: c# version wrapped in a SortableDropDownListmemberbanditking20-Apr-06 4:41 
GeneralRe: c# version wrapped in a SortableDropDownListmemberRyan McFarren14-Jul-06 6:25 
GeneralRe: c# version wrapped in a SortableDropDownListmemberashushere8-Jun-07 11:56 
GeneralRe: c# version wrapped in a SortableDropDownListmemberruileier18-Sep-07 21:15 
Questionhello!??sussnoahart24-Feb-04 22:05 
AnswerRe: hello!??sussAnonymous4-Aug-05 0:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 29 Sep 2003
Article Copyright 2003 by Domenic
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid