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

3 posts, 0 answers
  1. Mike
    Mike avatar
    3 posts
    Member since:
    Mar 2014

    Posted 01 Dec 2014 Link to this post

    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. Mike
    Mike avatar
    3 posts
    Member since:
    Mar 2014

    Posted 01 Dec 2014 Link to this post

    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. 




  3. Dimitar
    Admin
    Dimitar avatar
    2686 posts

    Posted 04 Dec 2014 Link to this post

    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.

     
Back to Top