Users Pricing

blog

Home Blogs How to remove duplicate item from DropDownList in asp.net – MindStick

How to remove duplicate item from DropDownList in asp.net

Ashok Aryan 11899 19 Feb 2016 Updated 14 Mar 2018
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




Ashok Aryan

Other


1 Comments


Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md