blog

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

How to remove duplicate item from DropDownList in asp.net

Ashok Aryan 10924 19-Feb-2016
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
How to remove duplicate item from DropDownList in asp.net

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

How to remove duplicate item from DropDownList in asp.net




Updated 14-Mar-2018

Leave Comment

Comments

Liked By