ListBox item removal issue

Hello Experts,

I have two Listboxes in my ASPX page. On page load  the first listbox(lballcolumns) lists all the items from the db after loading all items using the below code I am removing the items that all already in the  second Listbox(lbselected).

for (int i = lbselectedColumns.Items.Count-1; i >= 0; i--)
                if (lbselectedColumns.Items[i].Selected)

But somehow last item is not getting removed. For eg. if First Name is the last column in my dbtable, that is not getting removed. What may be the reason.

Please help
Who is Participating?
mmarinovConnect With a Mentor Commented:
Are you sure that
lballcolumns.Items.RemoveAt(0); will not remove it?
I think that the problem is in the indexing of the items

The problem could be that as you remove the items the value of lbselectedColumns.Items.Count is decreasing so you could infact just jump over items. Try doing a i-- inside the if clause.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.