SortedList order...

Posted on 2005-04-20
Last Modified: 2008-01-09
Using a SortedList to diplay the Months in a Dropdown List:

colMonth = New SortedList()
colMonth.Add( "1", "January" )
colMonth.Add( "2", "February" )
colMonth.Add( "3", "March" )
colMonth.Add( "4", "April" )
colMonth.Add( "5", "May" )
colMonth.Add( "6", "June" )
colMonth.Add( "7", "July" )
colMonth.Add( "8", "August" )
colMonth.Add( "9", "September" )
colMonth.Add( "10", "October" )
colMonth.Add( "11", "November" )
colMonth.Add( "12", "December" )

However, when the Dropdown list is displayed it goes in the order of:
      <option value="1">January</option>
      <option value="10">October</option>
      <option value="11">November</option>
      <option value="12">December</option>
      <option value="2">February</option>
      <option value="3">March</option>
      <option selected="selected" value="4">April</option>
      <option value="5">May</option>
      <option value="6">June</option>
      <option value="7">July</option>
      <option value="8">August</option>
      <option value="9">September</option>

Is there anyway of sorting this the right way?
Question by:gilkesy
    LVL 7

    Expert Comment

    Are you able to put in 01, 02, 03, 04..... ??

    If you're taking that value and doing a Convert.ToInt32 on the backend, should work fine.


    Author Comment

    I could put in 01, 02, 03.

    But I was using this routine to select the current date:

                Dim intMonth As Integer = Convert.ToString(month(now))
                for i = 1 to 12
                if intMonth = i then
                      dropStartMonth.SelectedValue = i
                end if

    This doesnt work if I use 01, 02, 03
    LVL 7

    Assisted Solution

    how about something like

    dropStartMonth.SelectedItem = Month(i)


    I totally know what you're saying with the sortedlist, but I haven't found it to be all that flexible.

    Author Comment

    Dim intMonth As Integer = Convert.ToString(month(now))

    for i = 1 to 12
    if intMonth = i then
          dropStartMonth.SelectedItem = Month(i)
    end if

    Results in:

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30311: Value of type 'Integer' cannot be converted to 'Date'.

    Source Error:


    Line 60:             for i = 1 to 12
    Line 61:             if intMonth = i then
    Line 62:                   dropStartMonth.SelectedItem = Month(i)
    Line 63:             end if
    Line 64:             next
    LVL 8

    Accepted Solution

    can the key object in the Add method be an int instead of a string?

    colMonth = New SortedList()
    colMonth.Add( 1, "January" )
    colMonth.Add( 2, "February" )
    colMonth.Add( 3, "March" )
    colMonth.Add( 4, "April" )

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity C# Stream to WorkBook 5 43
    Explain Unit of Work pattern 2 28
    call function 8 34
    crm written in / c# 1 26
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( 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…

    755 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

    26 Experts available now in Live!

    Get 1:1 Help Now