?
Solved

Trying to sort a Databound ComboBox?

Posted on 2014-09-18
6
Medium Priority
?
115 Views
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()
        Try
            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

        Try
            tbl.Columns.Add("SAMAcctName")
            tbl.Columns.Add("commonName")
            tbl.Columns.Add("email")

            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
                tbl.Rows.Add(row)
            Next

            ds.Tables.Add(tbl)
            EH.ErrorMessage = ""

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

        Return ds
    End Function

Open in new window

0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40331764
Hello,

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:

http://msdn.microsoft.com/en-us/library/zk13kdh0(v=vs.71).aspx
0
 

Author Comment

by:BlakeMcKenna
ID: 40332479
That didn't help.

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

Accepted Solution

by:
BlakeMcKenna earned 0 total points
ID: 40332546
I figured out how to use the "Sort" method...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

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

Author Comment

by:BlakeMcKenna
ID: 40332803
Sure,

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 = ""

        Try
            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
            Next

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

Open in new window

0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40341119
It works!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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