Sponsored Ad

Monday, April 5, 2010

How to Fill Dropdown with List of Months in C#

 

This utility will help you to fill dropdown with list of months. Developer need to pass the dropdown list in given month and dropdown will be filled with all twelve months.

You can customize this method and add only custom month just by modifying the month array list.

public static void GetMonth(ref DropDownList mDDMonth)
       {
           string[] MonthList = new string[12] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

           int MonthValue = 0;
           for (int i = 0; i < MonthList.Length; i++)
           {
               MonthValue = i + 1;
               mDDMonth.Items.Add(new ListItem(MonthList[i], MonthValue.ToString()));
           }
       }

2 comments:

  1. How about this one:

    foreach (String month in CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames)
    mDDMonth.Items.Add(new ListItem(month, month));

    2 line version.

    ReplyDelete
  2. awesome Paul Venneker

    ReplyDelete

Sponsored Ad

Development Updates