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....

20 comments:

  1. Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career.

    java training in chennai | java training in bangalore

    java online training | java training in pune

    java training in chennai | java training in bangalore

    ReplyDelete
  2. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    python training in pune
    python online training
    python training in OMR

    ReplyDelete
  3. Outstanding blog post, I have marked your site so ideally I’ll see much more on this subject in the foreseeable future.
    Blueprism training in marathahalli

    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  4. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. thanks a lot
    Java training in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Online Training

    ReplyDelete
  5. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us.



    oracle training in chennai

    oracle training in annanagar

    oracle dba training in chennai

    oracle dba training in annanagar

    ccna training in chennai

    ccna training in annanagar

    seo training in chennai

    seo training in annanagar

    ReplyDelete

Total Pageviews