Results 1 to 5 of 5

Thread: [RESOLVED] Problem sorting a combo

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Resolved [RESOLVED] Problem sorting a combo

    Using vb 2012 I bind the combo to a datatable (TABLE). I also add a new row <None>.

    Code:
    Dim row As DataRow
    row = TABLE.NewRow()
    row("EmployeeID") = 0
    row("EmployeeName") = "<None>"
    TABLE.Rows.Add(row)
    
    cboBuildingManager.DataSource = TABLE
    cboBuildingManager.DisplayMember = "EmployeeName"
    cboBuildingManager.ValueMember = "EmployeeID"
    This works perfectly. However, I would like <None> to appear at the top of the list (hence the spelling), but when I sort the combo all the values get mixed up. The selected value is for the item below the one chosen and then it wraps around to the top on the bottom choice. So I suspect I have to sort the datatable before binding it. Sorting the names when loading the table is simple with sql .. but how do I sort after adding <None>? Or perhaps there is a better way to add <None> to the combo list. If I have to leave it on the bottom of the combo list I suppose I can but I would prefer it to be at the top.

    TIA
    Ken

  2. #2
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Problem sorting a combo

    after you add your <None> row, dim a dataview, add your table to it, sort the view, and use that as your datasource

    Here's a link for reference
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  3. #3
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Problem sorting a combo

    Did you set the Sorted property of the combo box to true?
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Re: Problem sorting a combo

    Thank you for the quick reply. Setting the sorted property of the combo resulted in scrambled results. However, using a dataview and sorting that did the trick. I've never used a dataview so I never thought of it. Thank you! For anyone else reading this I changed the code to:

    Code:
            Dim row As DataRow
            row = TABLE.NewRow()
            row("EmployeeName") = "<None>"
            row("EmployeeID") = 0
            TABLE.Rows.Add(row)
    
            Dim dv As New DataView(TABLE)
            dv.Sort = "EmployeeName ASC"
    
            cboBuildingManager.DataSource = dv
            cboBuildingManager.DisplayMember = "EmployeeName"
            cboBuildingManager.ValueMember = "EmployeeID"
    Thanks again!!
    Ken

  5. #5
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: [RESOLVED] Problem sorting a combo

    The sorted property is sorting on the value, EmployeeID, column is probably why. Missed that sorry. Glad you got it working.
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width