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

Exception thrown when adding item to context menu after clearing bound list

2 Answers 88 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 01 Dec 2014, 03:09 PM
I have a GridView that is bound to a binding list

BindingList<DataElementMap> DataElementMaps { get; set; }

mappingsGridView.DataSource = DataElementMaps;

I am adding a menu item to the Context Menu using the following

 private void mappingsGridView_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
                
if (!e.ContextMenu.Items.Contains(_ceateTransformationMenuItem))
                {
                    e.ContextMenu.Items.AddRange(_ceateTransformationMenuItem);
                }
}

This works fine until I clear the data bound list.  (i.e. DataElementMaps.Clear(); ) 

After a clear is executed the above code throws the following exception
System.ArgumentException occurred
  Message=Cannot remove the specified item because it was not found in the specified Collection.
  Source=mscorlib
  StackTrace:
       at System.Collections.CollectionBase.System.Collections.IList.Remove(Object value)
       at Telerik.WinControls.RadElementCollection.CheckElementAlreadyAdded(RadElement element)
       at Telerik.WinControls.RadElementCollection.OnInsert(Int32 index, Object value)
       at System.Collections.CollectionBase.System.Collections.IList.Insert(Int32 index, Object value)
       at Telerik.WinControls.RadItemOwnerCollection.OnInsertComplete(Int32 index, Object value)
       at System.Collections.CollectionBase.System.Collections.IList.Add(Object value)
       at Telerik.WinControls.RadItemCollection.AddRange(RadItem[] value)



2 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 01 Dec 2014, 04:37 PM
I have found the cause of this error.  I was only instatiating the menu item once  for the lifetime of the grid view control.  I change the above code to 
private void mappingsGridView_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    if (!e.ContextMenu.Items.Contains(_ceateTransformationMenuItem))
     {
        _ceateTransformationMenuItem = new RadMenuItem("Create Transformation");
        _createTransformationMenuItem.Click += new EventHandler(CreateTransformationClick); 
        e.ContextMenu.Items.AddRange(_ceateTransformationMenuItem);               
      }
}

So it appears to me that the code that adds the menu item to the context menu items collection (CheckElementAlreadyAdded)  is looking at some other internal list to determine if the menu item exists and thus attempting to remove it from a list that from where it does not exist. 




0
Dimitar
Telerik team
answered on 04 Dec 2014, 11:52 AM
Hi Mike,

Thank you for writing.

In this case the item is disposed when the data source is cleared and this is why you get the exception. Please note that the all context menu items are disposed at some point, however you can create new item each time or check if the item is disposed:
void radGridView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)
{
    if (!e.ContextMenu.Items.Contains(myItem))
    {
        if (myItem.IsDisposed)
        {
            myItem = new RadMenuItem("My Item");
            myItem.Click += myItem_Click;
        }
        e.ContextMenu.Items.Add(myItem);
    }
}

Please let me know if there is something else I can help you with. 
 
Regards,
Dimitar
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or