Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two combo box in my form , I am adding and removing items on click on Items. But I got this error when I remove last item from combo box "InvalidArgument=Value of '0' is not valid for 'index'." I try to do some solution but still this error comes.Here is my code of removing item from combo box.
C#
private void cbSelectedItems_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                var s = (ComboBox)sender;
                if (s.SelectedItem != null)
                {
                    InvoiceItemBAL selectedRow = s.SelectedItem as InvoiceItemBAL;

                    Guid productId = selectedRow.id;
                    if (itemList.Where(q => q.id == productId).Count() > 0)
                    {
                        foreach (var item in itemList)
                        {
                            if (item.id == productId)
                            {
                                s.Items.Remove(item);
                                itemList.Remove(item);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please Select Item First");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Unhandled Exception");
                this.Close();
            }
        }
Posted

1 solution

You cannot remove items from a collection which is being used by a foreach
C#
foreach (var item in itemList)
{
    if (item.id == productId)
    {
        s.Items.Remove(item);
        itemList.Remove(item);
        break;
    }
}
Trying to do that will alter the collection, and that is not allowed.
Change your foreach to a for loop, running backwards through the collection and it should work.
 
Share this answer
 
Comments
Jigar Sangoi 8-Feb-14 11:34am    
I need to remove item from the list to make list updated. for and foreach loop doesn't make the change. Found some workaround by doing this things on button click i.e remove item from combo box
OriginalGriff 8-Feb-14 11:49am    
You can remove items from a list, but you can't remove items from a list that a foreach loop is processing on. Think about it: if you have a linked list, and you remove the current node, which is the "Next" node? Because the current node is no longer a part of the list, there is no logical "Next" node that follows the list through.
Jigar Sangoi 8-Feb-14 11:41am    
Itemlist is used in another form by get and set property. So If I don't remove the item from list then I will get an item in List which is removed by user.
Jigar Sangoi 8-Feb-14 11:59am    
Can you please help me to explore more why this happening. Because this same thing is achieved in button click event
OriginalGriff 8-Feb-14 12:07pm    
First, get rid of the foreach loop, and replace it with a for loop - if you run it from the Count - 1 to zero, you can safely delete the list items.
Then try it again, and see if the problem has gone away...

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