I am developing an application which I have added 2 combobox's (possibly not the best way but it works fine in the most part) which I import data into from a CSV file.
I have added code in so that users can add to this file, again this works fine. However I cannot get the "linked" combobox's to sort correctly.
Here is the code:

       Dim WebLinks = (From line In IO.File.ReadAllLines("C:\Test\URLList.csv") _
Where line.Length > 0 _
Let Items = line.Split(","c) _
Select New With _
{.URLName = Items(0), _
.URLLoc = Items(1) _
} _
).ToList

        For Each URLSCut In WebLinks
            Console.WriteLine("[{0} [{1}]", _
                              URLSCut.URLName, _
                              URLSCut.URLLoc _
                  )

        Next

        PageName.DataSource = WebLinks
        PageName.DisplayMember = "URLName"

        WebAdd.DataSource = WebLinks
        WebAdd.DisplayMember = "URLLoc"

If I then set PageName (aka ComboBox1) to "Sorted = Yes" on the combobox itself the 2 fall out of sequence. I only want to sort on PageName / ComboBox1.

Is there any way I can get it to sort either on import or once imported? Because I want the list to appear alphabetically, but don't want to have to manually sort it.
I've tried using .Sorted etc, but they give various errors or are non-functional.

Any advice greatfully received.

Recommended Answers

All 2 Replies

If you use the OrderBy method right before you cast to list, your list can be sorted by the urlname. Something like this should do:

    Dim WebLinks = (From line In IO.File.ReadAllLines("C:\Test\URLList.csv") _
                    Where line.Length > 0 _
                    Let Items = line.Split(","c) _
                    Select New With _
                    {.URLName = Items(0), _
                    .URLLoc = Items(1) _
                    } _
                    ).OrderBy(Function(x) x.URLName).ToList

To make sure it works I used a .csv file with sample data with this format: name,url. WebLinks ends up sorted by URLName.

I didn't realise this had posted when I reposted it, the answer has fixed the issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.