Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

ASP.net - delete certain items from a DropDownList

 I am using the following VB.net code to delete all the items in a DropDownList that
contain commas - the problem is that because items get deleted, the count of items
changes and the code errors out because it runs over the index count. Ho do I avoid this ?        

  For k As Integer = 0 To L.Items.Count - 1
                s = L.Items(k).Text
                If InStr(s, ",") > 0 Then
                    'Delete item because comma was in it
                    L.Items.Remove(L.Items.FindByValue(s))
                End If
            Next
ASP.NET

Avatar of undefined
Last Comment
Murray Brown
ASKER CERTIFIED SOLUTION
Avatar of Rick
Rick

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sara bhai
Sara bhai
Flag of India image

check this sample ... u need tp implement according to your need..

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;
}
}
}
Avatar of Rick
Rick

dim int as integer = l.items.count

for i as integer = int - 1 to 0 step - 1
     s = L.Items(i).Text       ' This is supposed to be "i", not "k"
                If InStr(s, ",") > 0 Then
                    'Delete item because comma was in it
                    L.Items.Remove(L.Items.FindByValue(s))
                End If
next
Avatar of Rick
Rick

You could also rewrite it like this:

for k as integer = l.items.count - 1 to 0 step - 1
   if l.items(k).text.contains(",") then
      l.items.remove(l.items(k)
   end if
next
Avatar of Rick
Rick

You could also rewrite it like this:

for k as integer = l.items.count - 1 to 0 step - 1
   if l.items(k).text.contains(",") then
      l.items.remove(l.items(k))     ' can't type today... forgot to close parentheses here  
   end if
next
Avatar of Murray Brown
Murray Brown
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

worked well. Thansk
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo