Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove listbox items, part of datasource

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hi,
I have googled and tek-tip searched to no avail.

.Net version is 2.0

I have a ListBox that is bound to a datatable. I need to allow the user to remove some of the items in this ListBox. When I do the following code:
Code:
ListBox.SelectedObjectCollection lsicSelectedUsers = lbUser_UsersSelected.SelectedItems;
            foreach (DataRowView drw in lsicSelectedUsers)
            {
                gdtSelectedUsers.Rows.Remove(drw.Row); 
            }
I get the following Exception :
Code:
InvalidOperationException:
List that this enumerator is bound to has been modifed.  An enumerator can only be used if the list has not been modified.

Something else I have tried was to place all the selected items into a datarow array and then remove each row that was in the array in the datatable. When I do that I get a DataRowNotInTableException.

I have not idea why it seems to difficult to remove these tiems successfully.

Any help is GREATLY appreciated.

Thanks,
RalphTrent
 
the message means that you cannot modify the collection while iterating through it. however I do not see why that is the message thrown with the code provided.

I would recommend a different approach though.
1. get all potential items from for the listbox
2. get all the items you want to remove
3. create a 3rd list of the filtered items you want to display
4. bind this 3rd list of filtered results to the listbox.

the idea is to perform all the data manipulation before pushing data to the GUI.



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Yeah, the exception is being thrown because you are modifying the collection while iterating through it.

If you want you can use a for loop to do the same thing. That should allow you to remove the row
 
Try using a for loop with SelectedIndices in reverse order (highest to lowest).

The problem occurs with for each because after removing a row, the enumerator has lost its place. Similarly if you use a for loop and interate forwards the list will be "renumbered" and you could end up with an out of bounds error.
 
Based on the responses I got and thank you for them, I have put the following code together:
Code:
ListBox.SelectedIndexCollection lsicSelectedUsers = lbORC_Users_Selected.SelectedIndices;
int liWhat = lsicSelectedUsers.Count - 1;
for(int i = liWhat; i > -1; i--)
{
	DataRow ldrSelectedUser = ((DataRowView)lbORC_Users_Selected.Items[lsicSelectedUsers[i]]).Row;
        gdtSelectedUsers.Rows.Remove(ldrSelectedUser);
}
gdtSelectedUsers is the database I am holding the selected users which is bound to my second listbox, which is where I am trying to remove the users from.

I am getting the index out of bounds error. when I execute this once successfully, lsicSelectedUsers goes from 3 to 1. I am selected to remove 3 users, hence the starting number is three, but after the first execution, i becomes 1 so I am not sure why it is remove 2 records in the first pass.

THIS IS DRIVING ME NUTS!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top