Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

When i move from list1 to list2, it will working fine.
but, meantime it will not be deleting from list1.

what is the problem in my code.
see code below.
-----------------

VB
If lstTot.SelectedIndex > -1 Then
            Dim _value As String = lstTot.SelectedItem.Value
            Dim _text As String = lstTot.SelectedItem.Text
            Dim item As New ListItem()
            item.Text = _text
            item.Value = _value
            For Each li As ListItem In lstTot.Items
                If li.Selected = True Then
                    lstEx.Items.Add(li.Text)
                    lstTot.Items.Remove(li.Text)
                End If
            Next
        End If
Posted
Updated 14-Jul-11 16:02pm
v2

After execution of the loop, bind lstTot once again. Hope that will resolve your problem. You didn't mention how lstTot got populated
 
Share this answer
 
In Remove method pass only 'li' in place of 'li.Text'
 
Share this answer
 
Comments
gani7787 14-Jul-11 7:03am    
Hi,

I am getting error as below if i use 'li' in place of 'li.Text'

Error : "Collection was modified; enumeration operation may not execute."

What is this...?
nagendrathecoder 15-Jul-11 3:05am    
This is not allowed, modification inside enumeration is not allowed on an object which is being enumerated.
This is not a right way to remove items from list. You may get errors.

Try like this:
For Each obj As Object In lstTot.SelectedItems
    lstEx.Items.Add(obj)
Next
While lstTot.SelectedIndices.Count > 0
      lstTot.Items.RemoveAt(lstTot.SelectedIndices.Item(0))
End While
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900