Solved

Trying to sort a Databound ComboBox?

Posted on 2014-09-18
6
109 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
Technology Partners: 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!

 
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Winform Module - What is the ASP.Net equiv 2 38
How to duplicate form in Visual Studio 2015 2 96
Generate Unique ID in VB.NET 21 101
vb.net deleting excel sheet in workbook 11 30
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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