Solved

Trying to sort a Databound ComboBox?

Posted on 2014-09-18
6
104 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
  • 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now