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
Solved

Trying to sort a Databound ComboBox?

Posted on 2014-09-18
6
108 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 36
get combo value in class 5 14
Looping through datagridview and dataset ? 6 24
Access Schema 6 21
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

766 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