Home > DeveloperSection > Blogs > How to remove duplicate item from DropDownList in asp.net

How to remove duplicate item from DropDownList in asp.net


C# ASP.Net 
Ratings:
0 Comment(s)
 733  View(s)
Rate this:
In this small blog i am going to demonstrate how to remove duplicate item from dropdownlist in asp.net.Create a sample page in Vs, Drag a dropdownlist put some item like below


Now Call the below code snippet to remove duplicate items from dropdownlist

 code snippet

  void RemoveDuplicateItems(DropDownList ddl)
    {
        for (int i = 0; i < ddl.Items.Count; i++)
        {
            ddl.SelectedIndex = i;
            string str = ddl.SelectedItem.ToString();
            for (int counter = i + 1; counter < ddl.Items.Count; counter++)
            {
                ddl.SelectedIndex = counter;
                string compareStr = ddl.SelectedItem.ToString();
                if (str == compareStr)
                {
                    ddl.Items.RemoveAt(counter);
                    counter = counter - 1;
                }
            }
        }
    }

Results






Don't want to miss updates? Please click the below button!

Follow MindStick