Thursday, February 07, 2008

I just thought this little piece of code was useful and I didn't find much in my google searches for doing this cleanly so here you go.

Creating a drop down list of the months in a year with their name and numeric value.

for (int i = 1; i <= 12; i++)
{
DateTime datetime = new DateTime(1, i, 1);
string month = datetime.ToString("MMMM");
dropdownlist.Items.Add(new ListItem(month, i.ToString()));
}