Solved

How to sort xml data element in a Combobox?

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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