Powered By Blogger

Monday, November 23, 2009

All Country List in a Dropdownlist c#

using System.Collections.Generic;
using System.Globalization;



Page_Load

{
PopulateCountryName(ddl_country);
}



private void PopulateCountryName(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());
}
}
List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>(objDic);
myList.Sort(

delegate(KeyValuePair<string, string> firstPair,

KeyValuePair<string, string> nextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);
foreach (KeyValuePair<string, string> val in myList)
{
dropDown.Items.Add(new ListItem(val.Key, val.Value));
}
}

No comments:

Post a Comment