Solved

How to sort xml data element in a Combobox?

Posted on 2011-09-09
4
322 Views
Last Modified: 2012-05-12
Hello,
I’m using the code below to load values from xml files to two combox boxes, how do I modify the code the sort by the “Name” element?
Thanks,
Victor
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Root/Link")
        Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/Donor")

        Dim donorlst As New DataTable()
        donorlst.Columns.Add("ID")
        donorlst.Columns.Add("Name")
        Dim reclst As New DataTable
        reclst.Columns.Add("ID")
        reclst.Columns.Add("Name")

        For Each n As XmlNode In lnk
            Dim rID As Integer = Val(n.ChildNodes(0).InnerText)
            Dim dID As Integer = Val(n.ChildNodes(1).InnerText)

            For Each node As XmlNode In RecName
                If node.ChildNodes(0).InnerText = rID.ToString Then
                    reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerText})
                    'Exit For
                End If
            Next

            For Each node As XmlNode In DonorName
                If node.ChildNodes(0).InnerText = dID.ToString Then
                    donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
                    'Exit For
                End If
            Next
        Next

        CmbDonor.DataSource = donorlst
        CmbDonor.DisplayMember = "Name"
        CmbDonor.ValueMember = "ID"

        CmbRec.DataSource = reclst
        CmbRec.DisplayMember = "Name"
        CmbRec.ValueMember = "ID"
0
Comment
Question by:vcharles
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:MikkelAStrojek
ID: 36513364
Have you tried something like this

donorlst.DefaultView.Sort = "Name ASC"
reclst.DefaultView.Sort = "Name ASC"

Open in new window

0
 
LVL 5

Accepted Solution

by:
MikkelAStrojek earned 500 total points
ID: 36513424
I've tested the following and it works:
        Dim donorlst As New DataTable()
        donorlst.Columns.Add("ID")
        donorlst.Columns.Add("Name")
        donorlst.Rows.Add(New Object() {"1", "One"})
        donorlst.Rows.Add(New Object() {"3", "Z"})
        donorlst.Rows.Add(New Object() {"2", "Two"})

        donorlst.DefaultView.Sort = "Name DESC"
        Me.ComboBox1.DataSource = donorlst
        Me.ComboBox1.DisplayMember = "Name"

Open in new window

So you need to set the DefaultView.Sort of your datatable to whatever sort order you need and you should be good to go
0
 

Author Comment

by:vcharles
ID: 36513435
Thanks for the code, I will get back to you tomorrow.
0
 

Author Closing Comment

by:vcharles
ID: 36514878
Thank You!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 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