Solved

How to sort xml data element in a Combobox?

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now