Getting cannot include items without a corresponding user interface element exception

12 posts, 0 answers
  1. Jerry Kurata
    Jerry Kurata avatar
    92 posts
    Member since:
    Feb 2010

    Posted 20 Jan 2011 Link to this post

    Hi,

    When I try to move columns into the front of my grid so they can be "frozen" I sometimes get this error
    "Range of Remove(0,8) cannot include items without a corresponding user interface element."  Any ideas?

    The code used to move a column is

    public void SetOrderIndex(MyDisplayColumn DispCol, int NewOrderIndex)
           {
               try
               {
                   int NextOrderIndex = 0;
                   foreach (MyDisplayColumn dc in this.Where(s => s != DispCol).OrderBy(s => s.OrderIndex))
                   {
                       if (NextOrderIndex == NewOrderIndex)
                       {
                           NextOrderIndex++;
                       }
                       dc.OrderIndex = NextOrderIndex++;
                   }
                   DispCol.OrderIndex = NewOrderIndex;
               }
               catch (Exception ex)
               {
                   string Message = ex.Message;            // trap the message for later handling
                   throw (ex);                             // rethrow the exception
               }
     
           }

    The purpose of the code is to create a hole in the OrderIndex sequence and drop the column into this hole.  The field OrderIndex is copied directly into the the GridViewColumn's OrderIndex.

    This works fine the first couple of times, but on the 4 or 5th movement the following exception /stack trace occurs:

    {System.InvalidOperationException: Range of Remove(0,8) cannot include items without a corresponding user interface element.
       at System.Windows.Controls.ItemContainerGenerator.Remove(GeneratorPosition position, Int32 count, Boolean isRecycling)
       at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IRecyclingItemContainerGenerator.Recycle(GeneratorPosition position, Int32 count)
       at Telerik.Windows.Controls.GridView.GridViewCellsPanel.CleanupRange(IList children, IItemContainerGenerator generator, Int32 startIndex, Int32 count)
       at Telerik.Windows.Controls.GridView.GridViewCellsPanel.VirtualizeChildren(List`1 blockList, IItemContainerGenerator generator)
       at Telerik.Windows.Controls.GridView.GridViewCellsPanel.GenerateAndMeasureChildrenForRealizedColumns(Size constraint)
       at Telerik.Windows.Controls.GridView.GridViewCellsPanel.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)}
        [System.InvalidOperationException]: {System.InvalidOperationException: Range of Remove(0,8) cannot include items without a corresponding user interface element.
       at System.Windows.Controls.ItemContainerGenerator.Remove(GeneratorPosition position, Int32 count, Boolean isRecycling)
       at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IRecyclingItemContainerGenerator.Recycle(GeneratorPosition position, Int32 count)
       at Telerik.Windows.Controls.GridView.GridViewCellsPanel.CleanupRange(IList children, IItemContainerGenerator generator, Int32 startIndex, Int32 count)
       at Telerik.Windows.Controls.GridView.GridViewCellsPanel.VirtualizeChildren(List`1 blockList, IItemContainerGenerator generator)
       at Telerik.Windows.Controls.GridView.GridViewCellsPanel.GenerateAndMeasureChildrenForRealizedColumns(Size constraint)
       at Telerik.Windows.Controls.GridView.GridViewCellsPanel.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)}
        Data: {System.Collections.ListDictionaryInternal}
        InnerException: null
        Message: "Range of Remove(0,8) cannot include items without a corresponding user interface element."
        StackTrace: "   at System.Windows.Controls.ItemContainerGenerator.Remove(GeneratorPosition position, Int32 count, Boolean isRecycling)\r\n   at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IRecyclingItemContainerGenerator.Recycle(GeneratorPosition position, Int32 count)\r\n   at Telerik.Windows.Controls.GridView.GridViewCellsPanel.CleanupRange(IList children, IItemContainerGenerator generator, Int32 startIndex, Int32 count)\r\n   at Telerik.Windows.Controls.GridView.GridViewCellsPanel.VirtualizeChildren(List`1 blockList, IItemContainerGenerator generator)\r\n   at Telerik.Windows.Controls.GridView.GridViewCellsPanel.GenerateAndMeasureChildrenForRealizedColumns(Size constraint)\r\n   at Telerik.Windows.Controls.GridView.GridViewCellsPanel.MeasureOverride(Size constraint)\r\n   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)"
  2. Jerry Kurata
    Jerry Kurata avatar
    92 posts
    Member since:
    Feb 2010

    Posted 24 Jan 2011 Link to this post

    Anyone have any thoughts?
  3. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 25 Jan 2011 Link to this post

    Hello Jerry Kurata,

    Is it possible that you are trying to access a column that is not in the view port?
    Can you try (just for the test) to disable the column virtualization (EnableColumnVirtualization="False") of RadGridView and see if the error still appears.

    Ideally, you can send us a sample project which reproduces the problem so we can debug it locally.


    Greetings,
    Veselin Vasilev
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  4. Jerry Kurata
    Jerry Kurata avatar
    92 posts
    Member since:
    Feb 2010

    Posted 25 Jan 2011 Link to this post

    Veselin,

    Thanks for the reply.  I will look into the out of viewport issue, but i am pretty sure we check before we try to access anything.  One thing that would help is being able to see further up the stack to get to my code.  Is there a setting that I can use to have the grid provide additional tracing or stack information?
  5. Jerry Kurata
    Jerry Kurata avatar
    92 posts
    Member since:
    Feb 2010

    Posted 26 Jan 2011 Link to this post

    I looked into the out of viewport issue and wanted to understand what you meant by viewport.  If you mean the portion of the grid that is visible then we definitely are operating on items not in the viewport since we can have dozens of columns and all columns need to be moved.  But I assume the grid should be able to handle this case since it is very common.
  6. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 27 Jan 2011 Link to this post

    Hi Jerry Kurata,

    Yes, that is what I meant by viewport. As I advised in the support ticket of yours - it would be great if you send us a sample project which exhibits the problem.

    Thank you

    Regards,
    Veselin Vasilev
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  7. jholt
    jholt avatar
    21 posts
    Member since:
    May 2007

    Posted 30 Jan 2011 Link to this post

    Hi,
    I've just had an identical problem. I think the problem is the timing between the grid receiving the NotifyCollectionChanged event from the binding.

    In my viewmodel, my data source was

    public ObservableCollection<T> Data
    {
        get{return _data;}
        private set{
            _data = value;
            RaisePropertyChanged("Data")
        }
    }


    This worked fine.

    HOWEVER - when I changed Data to the following - the problems you are having started (bizarely not for all RadGridViews).

    public ObservableCollection<T> Data
    {
        get        { return new ObservableCollection<T>(_repository.DataSource);
    }
     
    private void LoadingCompleted(Task task)
    {
        // do stuff to _repository.DataSource
       RaisePropertyChanged("Data")
    }

    I changed my code back to explicitly setting Data and the problem went away.

    I hope this helps

    Regards
    Jeremy

  8. Jerry Kurata
    Jerry Kurata avatar
    92 posts
    Member since:
    Feb 2010

    Posted 01 Feb 2011 Link to this post

    Jeremy,

    Thanks for the tip.  A couple of questions.

    1. Why are you returning a new collection?

    2. What calls the LoadingComplete method?  ie. when is it called.

    Thanks,

    jerry
  9. jholt
    jholt avatar
    21 posts
    Member since:
    May 2007

    Posted 01 Feb 2011 Link to this post

    Hi Jerry,

    _repository.DataSource is IEnumerable<T> - so I have to map it into an ObservableCollection<T>. I (foolishly) went down this road when I started writing the application and I can't work out a way to get out of it.
    My repository takes a UnitOfWork<IEnumerable<T>> - and I wrote a whole lot of superflous code in UnitOfWork to handle in-memory data and database data - for testing purposes. It was only ages afterwards that I realized that I should have cretaed a seperate UnitOfWork for testing purposes.
    So the problem I have is that I have to always coordinate the _repository.DataSource with Data<T> in the viewmodel.
    When it dawned on me how stupid I had been, I tried to make Data a readonly property in viewmodel just call down to the _repository.DataSource. This is where I discovered the same problem as you with the RadGridView.

    LoadingCompleted is called inside the ViewModel. All the crap about if(!UIContext != null) is that I can run the code in the Resharper/NUnit test runner - the TaskScheduler.FromCurrentSynchronizationContext() is always null in the test runner.

    To be honest I am getting sick and tired of LinqToSql. It works great for 99% of the time, but the time spent on the 1% that it doesn't is totally disproportionate to when it does - I'm referring here to DataContext - Attach and Detach, and the cache being out of sync with the data in the database.

    public virtual void Load()
           {
     
               InitializeLoad();
     
               if (UiSyncContext != null)
                   Task<ObservableCollection<T>>.Factory.StartNew(() => GetRepositoryData(LoadParametersPredicate))
                       .ContinueWith(LoadingCompleted, CancellationToken.None, TaskContinuationOptions.None, UiSyncContext);
               else
               {
                   Task<ObservableCollection<T>>.Factory.StartNew(() => GetRepositoryData(LoadParametersPredicate))
                       .ContinueWith(LoadingCompleted);
               }
           }


    Best regards
    Jeremy
  10. Jerry Kurata
    Jerry Kurata avatar
    92 posts
    Member since:
    Feb 2010

    Posted 02 Feb 2011 Link to this post

    Jeremy,

    Thanks again.

    We may have this under control.  We moved the code that reorders the columns until later in the code and the problems seems to have disappeared (at least for now).  I wonder if there are async operations that occur with the grid binding and they had not completed prior to our resetting the column order.   Or,  perhaps they started before and finished after our code that altered the column order.

    The problem is very strange and does not fill me with confidence that we understand the issue.
  11. Martin Roussel
    Martin Roussel avatar
    246 posts
    Member since:
    Jan 2010

    Posted 15 Dec 2011 Link to this post

    Hi everyone,

    sorry for bringing up a old post but i got the same problem over and over again (but it's not everytime)

    I got the error when my grid was refreshing its content after the delete (grid.ItemsSource = Newcol) and it also happened once on grid.ItemsSource = Nothing .

    is there any known fix for this? Im using Q3 2010 btw

    TIA
  12. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 16 Dec 2011 Link to this post

    Hello,

     Can you try some of our more recent builds in your scenario? Our latest official version is Q3 2011. 

    Kind regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top