I need to clear drpAddressTypes dropdown values except the first item and bind again that dropdownlist.
drpAddressTypes.Items.Clear();
var lstAddressTypes = repository.GetAddressTypes(userId);
if (lstAddressTypes != null)
{
foreach (var item in lstAddressTypes)
{
var addressType = new ListItem(item, item);
drpAddressType.Items.Add(addressType);
}
}
When I am using drpAddressTypes.Items.Clear(); it is clearing all items. How can I clear all items except the first item.
Thanks in advance. :)
You could retrive the firstitem and then clear the list and add the item again.
var firstitem = drpAddressType.Items[0];
drpAddressType.Items.Clear();
drpAddressType.Items.Add(firstitem);
Use Items.RemoveRange(1, items.Count-1)
..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With