5,445,109 members and growing! (14,945 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate

Sort Method for an ASP.NET DropDownList

By Domenic

This code will show you how to implement a sort feature on an ASP.NET DropDownList control.
VB, .NET, Win2K, WinXP, Win2003, Windows, Visual Studio, ASP.NET, Dev

Posted: 28 Sep 2003
Updated: 28 Sep 2003
Views: 115,705
Bookmarked: 19 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 3.62 Rating: 3.25 out of 5
5 votes, 38.5%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 15.4%
4
6 votes, 46.2%
5

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


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
Occupation: Web Developer
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThanks!memberdsilk11:52 3 Jul '08  
GeneralErrormemberhalliej2x22:45 1 Aug '06  
GeneralRe: ErrormemberDomenic3:27 2 Aug '06  
GeneralRe: Errormemberhalliej2x20:16 2 Aug '06  
GeneralRe: ErrormemberDomenic10:41 3 Aug '06  
QuestionRe: ErrormemberGregorio Cybill18:58 11 Aug '08  
GeneralRe: ErrormemberMohammed Aziz Elnahrawi21:31 5 Oct '06  
GeneralShortcut using CopyTo method on prealloced arraymemberhafthor6:22 27 Feb '06  
GeneralGood code, but let's make it even bettermemberHamidTheProgrammer11:34 9 Aug '05  
GeneralRe: Good code, but let's make it even bettermemberDomenic3:08 10 Aug '05  
GeneralWould just like to say...sussAnonymous1:51 4 Aug '05  
GeneralRe: Would just like to say...memberDomenic3:18 4 Aug '05  
GeneralSyntax errormemberchatelainj9:41 1 Sep '04  
GeneralRe: Syntax errorsussAnonymous9:21 3 May '05  
Generalc# version wrapped in a SortableDropDownListmemberSteinar Herland23:57 2 Apr '04  
GeneralRe: c# version wrapped in a SortableDropDownListmemberPaul Rogan1:32 10 Jan '06  
GeneralRe: c# version wrapped in a SortableDropDownListmemberbanditking5:41 20 Apr '06  
GeneralRe: c# version wrapped in a SortableDropDownListmemberRyan McFarren7:25 14 Jul '06  
GeneralRe: c# version wrapped in a SortableDropDownListmemberashushere12:56 8 Jun '07  
GeneralRe: c# version wrapped in a SortableDropDownListmemberruileier22:15 18 Sep '07  
Generalhello!??sussnoahart23:05 24 Feb '04  
GeneralRe: hello!??sussAnonymous1:45 4 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Sep 2003
Editor: Smitha Vijayan
Copyright 2003 by Domenic
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project