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;
}
}
}
}
Manish Kumar
05-Jun-2017I am grateful to you, for sharing a post like this.