Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done
ASP.NET Sort List Control
 
    Protected Sub btnLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeft.Click
        While (Me.lsRight.Items.Count > 0 And Me.lsRight.SelectedItem IsNot Nothing)
            Dim selectedItem As ListItem = Me.lsRight.SelectedItem
            selectedItem.Selected = False
            Me.lsLeft.Items.Add(selectedItem)
            Me.lsRight.Items.Remove(selectedItem)
        End While
        SortListControl(lsLeft, True)
        SortListControl(lsRight, True)
    End Sub
 
    Protected Sub btnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRight.Click
        While (Me.lsLeft.Items.Count > 0 And Me.lsLeft.SelectedItem IsNot Nothing)
            Dim selectedItem As ListItem = Me.lsLeft.SelectedItem
            selectedItem.Selected = False
            Me.lsRight.Items.Add(selectedItem)
            Me.lsLeft.Items.Remove(selectedItem)
        End While
        SortListControl(lsLeft, True)
        SortListControl(lsRight, True)
    End Sub
    Sub SortListControl(ByRef SourceList As ListControl, ByVal Ascending As Boolean)
        'sorts listcontrols
        Dim array1 As New ArrayList
        Dim loop1 As Integer
        For loop1 = 0 To SourceList.Items.Count - 1
            array1.Add(SourceList.Items(loop1))
        Next
 
        Dim myComparer = New SortListArray(Ascending)
        array1.Sort(myComparer)
 
        SourceList.Items.Clear()
        For loop1 = 0 To array1.Count - 1
            SourceList.Items.Add(array1(loop1))
        Next
    End Sub
 
    Public Class SortListArray
        Implements IComparer
        Private _Ascending As Boolean = True
        Public Sub New()
        End Sub
        Public Sub New(ByVal Ascending As Boolean)
            _Ascending = Ascending
        End Sub
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            If _Ascending Then
                Return New CaseInsensitiveComparer().Compare(x.ToString, y.ToString)
            Else
                Return New CaseInsensitiveComparer().Compare(y.ToString, x.ToString)
            End If
        End Function
    End Class
Posted on Thursday, February 14, 2008 7:03 AM GridView, Datagrid, DataList & Repeater | Back to top


Comments on this post: ASP.NET Sort List Control

# re: ASP.NET Sort List Control
Requesting Gravatar...
Thanks a lot for sharing this with us, was a really interesting post and very helpful.
Left by flower canvas on Jul 08, 2011 7:20 AM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net