Pages

Tuesday 27 November 2012

How to bind all the currencies list to a dropdownlist in Asp.net ?

In the following example, I am explaining how to bind all the currencies list to a Dropdownlist in Asp.Net.

Step-1: Add a dropdownlist to your page(ddlCurrency).

Step-2: Add 'using System.Globalization;' namespace to your .aspx.cs.

Step-3: Write the following method to select currencies and bind to dropdownlist.


public void GetCurrenciesList()
        {
            foreach (CultureInfo item in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
            {
                if (item.IsNeutralCulture != true)
                {
                    RegionInfo region = new RegionInfo(item.LCID);
                    string CurrencyName = region.CurrencyEnglishName;
                    string CurrenctSymbol = region.ISOCurrencySymbol;
                    ListItem li = new ListItem(CurrencyName + "(" + CurrenctSymbol + ")", CurrenctSymbol);
                    //** To check whether the Currency has already been added to the list or not ***//
                    if (ddlCurrency.Items.Count > 0)
                    {
                        int i = 0;
                        foreach (ListItem Curr in ddlCurrency.Items)
                        {
                            if (Curr.Value.Trim().ToLower() == li.Value.Trim().ToLower())
                            {
                                i++;
                            }
                        }
                        if (i == 0)
                        {
                            ddlCurrency.Items.Add(li);
                        }
                    }
                        //***********************************************************************//
                    else
                    {
                        ddlCurrency.Items.Add(li);
                    }
                }
            }
            //*************** To sort the dropdownlist items alphabatically *************//
            List<ListItem> listCopy = new List<ListItem>();
            foreach (ListItem item in ddlCurrency.Items)
            {
                listCopy.Add(item);
            }
            ddlCurrency.Items.Clear();
            foreach (ListItem item in listCopy.OrderBy(item => item.Text))
            {
                ddlCurrency.Items.Add(item);
            }
            //**************************************************************************//
            ddlCurrency.Items.Insert(0, "Select");
        }

Step-4: Call this method whenever you required to bind your dropdownlist.

Output: 


Your valuable comments will be highly appreciated. 
Regards,
Prajanuranjan....

Total Pageviews