SIGN UP MEMBER LOGIN:    
ARTICLE

Bind Countries to a DropDownList in ASP.NET

Posted by Suthish Nair Articles | ASP.NET Programming August 24, 2010
How to get list of all countries and bind to a dropdown list in ASP.NET.
Reader Level:
Download Files:
 

How to get list of all countries and bind to a dropdown list in ASP.NET

Create an ASP.NET application and add a DropDownList to the page something like this.

<asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList>

 

Now call this GetCountryList method on your page load that will bind and display countries in the DropDownList.

 

public List<string> GetCountryList()

    {

      List<string> list = new List<string>();

      CultureInfo[] cultures =

                  CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures | 
                  CultureTypes
.SpecificCultures);

      foreach (CultureInfo info in cultures)

      {

          RegionInfo info2 = new RegionInfo(info.LCID);

          if (!list.Contains(info2.EnglishName))

          {

              list.Add(info2.EnglishName);

          }

      }

      return list;

    }

 

ddlLocation.DataSource = GetCountryList();

        ddlLocation.DataBind();

ddlLocation.Items.Insert(0, "Select");

Note: Updated attachement contains code for orderby the list using LINQ.

Login to add your contents and source code to this article
share this article :
post comment
 

Ive given the reply next to his comments.. refer link posted..

Posted by Suthish Nair Sep 24, 2010

unfortunately your example is't working in my system...same problem i am facing which is mr. purushottam facing.

Posted by Raj Kumar Sep 24, 2010

Thanks for sharing Raj..

But my example also use to reverse sort the list.

Posted by Suthish Nair Sep 21, 2010

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


 public List<string> GetCountryList()
    {
        List<string> list = new List<string>();
     //   CultureInfo[] cultures =  CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures | CultureTypes.SpecificCultures);     

 
        foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            RegionInfo info2 = new RegionInfo(info.LCID);
            if (!list.Contains(info2.EnglishName))
            {
                list.Add(info2.EnglishName);
                list.Sort();
                
            }
        }
        return list;
    }



ddlCountry.DataSource = GetCountryList();
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, "Select");

Posted by Raj Kumar Sep 21, 2010

Nice Article, Interesting One.. thank you..

Posted by MAC Sep 01, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor