Populate country dropdown of you Form automatically using C# code

Posted by Raja under C# category on | Views : 30473
Generally while working on Registration or submission form we need to populate all the country name dropdownlist, copying pasting is difficult task in the DropDownList. Below is the function that populates the DropDownList that is passed to this function.

You will need to use following namespaces

using System.Configuration;
using System.Globalization;

/// <summary>
/// populate country name
/// </summary>
/// <param name="dropDown"></param>
public static void GetCountryNames(DropDownList dropDown)
Hashtable h = new Hashtable();

Dictionary<string, string> objDic = new Dictionary<string, string>();
foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name);
if (!objDic.ContainsKey(objRegionInfo.EnglishName))
objDic.Add(objRegionInfo.EnglishName, objRegionInfo.TwoLetterISORegionName.ToLower());

SortedList<string, string> sortedList = new SortedList<string, string>(objDic);

foreach (KeyValuePair<string, string> val in sortedList)
dropDown.Items.Add(new ListItem(val.Key, val.Key));

dropDown.Items.Insert(0, new ListItem("Select", "Select"));
dropDown.Items.Insert(1, new ListItem("Other Country", "Other"));

Call this function like this

Hope this will be helpful

Comments or Responses

Posted by: Puneet20884 on: 5/18/2010 Level:Bronze | Status: [Member]

It's really a very useful code snippet !!
Thanks for sharing here !!

Posted by: DrMDodd on: 9/2/2010 Level:Starter | Status: [Member] | Points: 10
I have rewritten the code into vb, but it appears that RegionInfo seems to have been truncated in VS 2010, and doesn't contain Englishname or ISORegionName. If you could offer some assistance I would appreciate it. Regards,

Posted by: DrMDodd on: 9/2/2010 Level:Starter | Status: [Member] | Points: 10
My fault, some how VS2010 had created a Class RegionInfo

Login to post response