Trying to sort a Databound ComboBox?

Posted on 2014-09-18
Last Modified: 2014-09-24
I know that you can't set the "sorted" property for a Databound ComboBox, however, I'm using a DataTable as the DataSource for the ComboBox. I guess I need some sort of Index for the DataTable? Here is what I have so far.

   Private Sub LoadCombo_NewUsers()
            dsUsers = LoadNewUsersComboBox(tblLDAP)

            If dsUsers.Tables(0).Rows.Count > 0 Then
                cmbUserName.DataSource = dsUsers.Tables(0)
                cmbUserName.DisplayMember = dsUsers.Tables(0).Columns(1).ColumnName
                cmbUserName.ValueMember = dsUsers.Tables(0).Columns(0).ColumnName
                cmbUserName.SelectedIndex = -1
            End If

            EH.ErrorMessage = ""

        Catch ex As Exception
            EH.ErrorMessage = "LoadCombo_ExistingUsers() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
    Public Shared Function LoadNewUsersComboBox(ByVal tbl As DataTable) As DataSet
        Dim row As DataRow
        Dim ds As New DataSet


            For x As Integer = 0 To arrStcLDAP.Count - 1
                row = tbl.NewRow
                row("commonName") = arrStcLDAP(x).commonName
                row("SAMAccountName") = arrStcLDAP(x).samAcctName
                row("email") = arrStcLDAP(x).email

            EH.ErrorMessage = ""

        Catch ex As Exception
            EH.ErrorMessage = "LoadNewUsersComboBox() - " & ex.Message & "~E"
        End Try

        Return ds
    End Function

Open in new window

Question by:BlakeMcKenna
  • 4
  • 2
LVL 15

Expert Comment

by:Haris Djulic
ID: 40331764

when binding the data source to combo box it is recommended that you do the sorting on the data source instead in  the combo box since it can cause some unwanted results (i.e. sort display member and value members separately)...

 In your case you can try to implement sorting on the data set. In the link you have more details:

Author Comment

ID: 40332479
That didn't help.

Do you know anything about the "DirectorySearcher.Sort" Method?

Accepted Solution

BlakeMcKenna earned 0 total points
ID: 40332546
I figured out how to use the "Sort" method...
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 15

Expert Comment

by:Haris Djulic
ID: 40332622
Can you post the solution for future reference?

Author Comment

ID: 40332803

I'm basically sorting the selected properties of the LDAP into a DataTable.

    Public Sub GetLDAPUsers()
        Dim directory As DirectoryEntry = New DirectoryEntry("LDAP://" & gstrDomainName)
        Dim findUser As DirectorySearcher = New DirectorySearcher(directory)
        Dim arr() As String
        Dim xCnt As Integer = 0
        Dim strValue As String = ""

            EH.ErrorMessage = ""

            findUser.Filter = "(&(SAMAccountName=*)(objectcategory=user))"

            Dim results As SearchResultCollection = findUser.FindAll

            findUser.Sort.Direction = SortDirection.Ascending   ------------> I just added this line
            findUser.Sort.PropertyName = "commonName"      ------------> I just added this line

            For Each result As SearchResult In results
                Dim Value As Integer = result.Properties.Item("useraccountcontrol")(0)

                Value = Value And &H2
                arr = Split(result.Properties.Item("name")(0), " ")

                If Value = 0 Then
                    If arr.Length > 1 Then
                        ReDim Preserve arrStcLDAP(xCnt)
                        arrStcLDAP(xCnt).commonName = result.Properties.Item("cn")(0).ToString '.ToUpper
                        arrStcLDAP(xCnt).samAcctName = result.Properties.Item("SAMAccountName")(0).ToString '.ToUpper
                        arrStcLDAP(xCnt).email = result.Properties.Item("mail")(0).ToString '.ToUpper
                        xCnt += 1
                    End If
                End If

        Catch ex As Exception
            EH.ErrorMessage = "modMain/GetLDAPUsers() - " & ex.Message & "~E"
        End Try
    End Sub

Open in new window


Author Closing Comment

ID: 40341119
It works!

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Web Form VB.Net  import CSV 4 39
Expression Evaluater 3 27
VB.NET 2008 Winforms Signing 13 20
get combo value in class 5 9
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question