Solved

How to sort xml data element in a Combobox?

Posted on 2011-09-09
4
323 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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