Wednesday, 13 February 2013

Get a list of countries for a dropdown

This is a surprisingly difficult question to answer, as it's not all as simple as just adding the System.Globalization namespace and getting stuck in.

It appears that there are in fact a large amount of incorrect / incomplete answers on the subject.

There is a web service that you could add and call which may prove to be more up-to-date than the .NET framework itself, but the easiest, fastest and WORKING method of adding countries to a drop-down is to use the method mentioned in this StackOverflow question:

using System.Collections.Generic;
using System.Globalization;
public static IEnumerable<string> GetCountryList()
{
List<string> cultureList = new List<string>();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in cultures)
{
RegionInfo region = new RegionInfo(culture.LCID);
if (!(cultureList.Contains(region.EnglishName)))
{
cultureList.Add(region.EnglishName);
}
}
return cultureList;
}
It's a life-saver!

No comments: