how to merge same values when binding the dropdownlist?
when i bind the dropdownlist as a result i get 2 same values (like 45, 45 ,24 ,24) but
i only want (45,24 ).
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjeeb LenkaPosted Jul 11, 2013, 11:54 AM
try this code
public static 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;
}
}
}
}
call this method by passing the dropdownlist id.
ex- RemoveDuplicateItems(ddlemp);
Iftikar HussainPosted Jul 11, 2013, 11:06 AM
Can you please provide your code? If you are binding from sql query then you can use
Select distinct col1,col1 from table1
Regards,
Iftikar