This is a migrated thread and some comments may be shown as answers.

comparing 2 listview items error

1 Answer 50 Views
ListView
This is a migrated thread and some comments may be shown as answers.
ahmed
Top achievements
Rank 1
ahmed asked on 18 Jan 2021, 05:02 PM

i'm trying to compare 2 listview items to check for duplicates

 

one listview is in a form and the other one is in a different open form

 

i get this error :

 

System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'

 

how to solve this error please 

invoicesnew master = (invoicesnew)Application.OpenForms["invoicesnew"];
            if (master.radListView1.Items.Count == 0)
            {
                foreach (ListViewDataItem a1234 in radListView1.Items)
                {
                    ListViewDataItem item = new ListViewDataItem();
                    item.SubItems.Add(a1234["Column 0"].ToString());
                    item.SubItems.Add(a1234["Column 1"].ToString());
                    item.SubItems.Add(a1234["Column 2"].ToString());
                    item.SubItems.Add(a1234["Column 3"].ToString());
                    item.SubItems.Add(a1234["Column 4"].ToString());
                    item.SubItems.Add(a1234["Column 5"].ToString());
                    item.SubItems.Add(a1234["Column 6"].ToString());
                    item.SubItems.Add(a1234["Column 7"].ToString());
                    master.radListView1.Items.Add(item);
                    this.Close();
                }
            }
            else
            {
                foreach (ListViewDataItem item in radListView1.Items)
                {
                    foreach (ListViewDataItem a1234 in master.radListView1.Items)
                    {
                        if (item["Column 0"].ToString() == a1234["Column 0"].ToString() && item["Column 1"].ToString() == a1234["Column 1"].ToString())
                        {
 
                            master.radListView1.FilterDescriptors.Clear();
                            master.radListView1.FilterDescriptors.LogicalOperator = FilterLogicalOperator.Or;
                            master.radListView1.FilterDescriptors.Add("Column 0", FilterOperator.Contains, item["Column 0"].ToString());
                            master.radListView1.FilterDescriptors.Add("Column 1", FilterOperator.Contains, item["Column 1"].ToString());
                            master.radListView1.EnableFiltering = true;
                            master.radListView1.SelectedIndex = 0;
 
                            decimal totalin = Convert.ToDecimal(item["Column 7"].ToString());
                            decimal countin = Convert.ToDecimal(item["Column 5"].ToString());
                            decimal totalin1 = Convert.ToDecimal(a1234["Column 7"].ToString());
                            decimal countin1 = Convert.ToDecimal(a1234["Column 5"].ToString());
                            alltotal = (totalin1 + totalin).ToString();
                            allcount = (countin1 + countin).ToString();
                            List<ListViewDataItem> saveSelectedItems = new List<ListViewDataItem>();
 
                            foreach (ListViewDataItem eachItem in master.radListView1.SelectedItems)
                            {
                                saveSelectedItems.Add(eachItem);
                            }
 
                            foreach (ListViewDataItem item3 in saveSelectedItems)
                            {
                                master.radListView1.Items.Remove(item3);
                            }
 
 
 
 
                            ListViewDataItem item1 = new ListViewDataItem();
                            item1.SubItems.Add(item["Column 0"].ToString());
                            item1.SubItems.Add(item["Column 1"].ToString());
                            item1.SubItems.Add(item["Column 2"].ToString());
                            item1.SubItems.Add(item["Column 3"].ToString());
                            item1.SubItems.Add(item["Column 4"].ToString());
                            item1.SubItems.Add(allcount);
                            item1.SubItems.Add(price);
                            item1.SubItems.Add(alltotal);
                            master.radListView1.Items.Add(item1);
                          
 
                        }
                        else
                        {
                            master.radListView1.EnableFiltering = false;
                            calculate();
                            ListViewDataItem item1 = new ListViewDataItem();
                            item1.SubItems.Add(radLabel13.Text);
                            item1.SubItems.Add(radLabel29.Text);
                            item1.SubItems.Add(radLabel15.Text);
                            item1.SubItems.Add(radDropDownList3.Text);
                            item1.SubItems.Add(radLabel24.Text);
                            item1.SubItems.Add(radMaskedEditBox3.Text);
                            item1.SubItems.Add(price);
                            item1.SubItems.Add(total);
                            master.radListView1.Items.Add(item1);
                            this.Close();
                        }
                    }
                }
            }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 Jan 2021, 05:48 AM

Hello, Ahmed, 

According to the provided code snippet and the specified error message that you are facing, it seems that you are using a foreach loop and add items to RadListView while you are iterating its items. Hence, the collection is being modified while enumerating.

The problem is not related to RadListView and the Telerik UI for WinForms suite. The following StackOverflow thread is quite useful on this topic: https://stackoverflow.com/questions/604831/collection-was-modified-enumeration-operation-may-not-execute   

You can add items outside the foreach loop or make a copy of the RadListView.Items collection and don't iterate the same collection that you are modifying.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
ListView
Asked by
ahmed
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or