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

Items collection must be empty before using ItemsSource

6 Answers 368 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marc Roussel
Top achievements
Rank 2
Marc Roussel asked on 31 Jan 2011, 06:17 PM
Hi,

Are these statements valid in a WCF call back to populate the grid ?  If the message is saying I have to issue a rgvTimes.Clear() it doesn'T make sence as I never had to do this in any of my applications to date.

_Times = e.Result;
rgvTimes.ItemsSource = _Times;
rgvTimes.Rebind();

As this gives me the error as follow and I'm unable to figure out why, where and what I have to look for to solve that problem

Items collection must be empty before using ItemsSource.
  
   à Telerik.Windows.Data.DataItemCollection.CheckInternalViewIsNotUsed()
   à Telerik.Windows.Data.DataItemCollection.SetItemsSource(IEnumerable source)
   à Telerik.Windows.Controls.GridView.GridViewDataControl.<>c__DisplayClass48.<Bind>b__47()
   à Telerik.Windows.Controls.CursorManager.PerformTimeConsumingOperation(FrameworkElement frameworkElement, Action action)
   à Telerik.Windows.Controls.GridView.GridViewDataControl.Bind(Object newValue)
   à Telerik.Windows.Controls.GridView.GridViewDataControl.OnItemsSourceChanged(Object oldValue, Object newValue)
   à Telerik.Windows.Controls.DataControl.OnItemsSourcePropertyChanged(DependencyObject origin, DependencyPropertyChangedEventArgs args)
   à Telerik.Windows.PropertyMetadata.<>c__DisplayClass1.<Create>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
   à System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   à System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   à System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   à Foo.MainPage._Proxy_UpdateTempsCompleted(Object sender, UpdateTempsCompletedEventArgs e)

Is there something wrong in my code above ?  What should I do to understand and see what's going on ?

6 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 03 Feb 2011, 08:34 AM
Hello Marc Roussel,

That message snows if you have used the grid in unbound mode - manually adding items to its Items collection without assigning ItemsSource. Is there such a code in your application?


Regards,
Milan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Marc Roussel
Top achievements
Rank 2
answered on 03 Feb 2011, 09:47 AM
Hi,

No, all the grids in the application are just populated once when the application starts and it's in the wcf call back.
I have 4 grids in total and my way of doing things is that I have a private collection for each grid that I keep intact which get populated in the wcf call back just before assigning it to the ItemsSource of the grid as shown in the code above in a previous message.

I did remove the Rebind(); for each of them to see if this could be the cause.  Even if it's not that,  I don't even know why I added it anyway I don't thing it's needed as after removing them I had no trouble but this error that occurs once in a while.

Prety weird tho that this part of the code works once and after a while, it gets this error.
0
Marc Roussel
Top achievements
Rank 2
answered on 06 Apr 2011, 05:01 PM
In fact, here's where it gives the error which is a call back from a call to a WCF service

private void _Proxy_AddNewTimeCompleted(object sender, AddNewTimeCompletedEventArgs e)
{
    ExceptionHandler(e.Error);
  
    if (e.Result.Message != null)
        if (e.Result.Message != "")
        {
            PolyrolGenerics.ShowCursor(this, false, false);
            ExceptionHandler(new Exception(e.Result.Message));
        }
  
    try
    {
        if (_Times != null)
        {
            if (_Times.Count > 0)
            {
                if (_Times.Where(t => t.Id == 0).FirstOrDefault() == null)
                    _Times.Add(e.Result);
                else
                    _Times.Where(t => t.Id == 0).FirstOrDefault().Id = e.Result.Id;
  
                rgvTimes.ItemsSource = null;
                rgvTimes.ItemsSource = _Times;
                //rgvTimes.Rebind();
            }
            else
            {
                rgvTimes.ItemsSource = null;
                //rgvTimes.Rebind();
            }
        }
        else
        {
            rgvTimes.ItemsSource = null;
            //rgvTimes.Rebind();
        }
    }
    catch (Exception ex)
    {
        ExceptionHandler(ex);
    }
  
    PolyrolGenerics.ShowCursor(this, false, false);
}

as you can see, I tried a few things in there that does not prevent the error message to show.  The grid is bound at a private collection _Times.  When I'm in this call back I check if it was and update that was made to one of the element of the collection and if so I update it else I add it to the collection and then I assign the collection again on the grid.  What am I doing wrong ?

0
Marc Roussel
Top achievements
Rank 2
answered on 06 Apr 2011, 05:14 PM
I think I found why.

I completed wiped out the call back and it seems that any addition made to the grid is also added to the collection and any update is also updated on the collection hence no need for manipulating the collection on the call back

Is that right ?
0
Milan
Telerik team
answered on 07 Apr 2011, 05:19 PM

Hello Marc Roussel,

As long as your collection supports notifications (INotifyCollectionChanged) any modifications executed through the grid will be propagated to the original collection. 



All the best,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Marc Roussel
Top achievements
Rank 2
answered on 08 Apr 2011, 12:59 PM
So since it is a ObservableCollection the Notification is implemented.

The only thing needed however is when you add something in the grid, the object you add doesn't have the primary key id as this is when it is added at the server side hence we need to get back in the call back and search the collection to set the id that was just added.  Am I right ?

Also can you tell me if I change something in the collection it is immediatally reflected on the grid ?
Let say I change a visual value on the grid but by the collection will the visual be there automatically or I have to do something more ?

Sorry a few questions at the same time.
Tags
GridView
Asked by
Marc Roussel
Top achievements
Rank 2
Answers by
Milan
Telerik team
Marc Roussel
Top achievements
Rank 2
Share this question
or