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

InvalidOperationException on DataFilter

1 Answer 71 Views
DataFilter
This is a migrated thread and some comments may be shown as answers.
Gen
Top achievements
Rank 1
Gen asked on 22 Feb 2011, 05:39 PM
Hi,
I get an InvalidOperationException when I try to apply a filter descriptors to my RadGridView or RadTreeListView with Silverlight 4.

I have implemented the code from this link to save and load the filter in my database, and it work like a charm in a seperate page. My problem is when I try to apply that same code to the filter on my main grid. Here is an example of my code:
XAML:
<telerik:RadDataFilter Name="radDataFilter2" Margin="0" Height="1" Grid.Row="0"
    Source
="{Binding Path=PagedSource, ElementName=radDataPager2}" Visibility="Collapsed"/>
<telerik:RadGridView x:Name="radGridView"
    ItemsSource
="{Binding Path=FilteredSource, ElementName=radDataFilter2}"
    SelectedItem
="{Binding Path=CurrentItem, Mode=TwoWay}" Height="436" 
    RowDetailsVisibilityMode
="Collapsed" AutoGenerateColumns="False"
    HorizontalAlignment
="Left" Grid.Row="0" Width="976" RowStyle="{StaticResource DraggedRowStyle}"
    dragAndDrop:RadDragAndDropManager.AllowDrop
="True" >
    ...
</telerik:RadGridView>
<telerik:RadDataPager x:Name="radDataPager2" PageSize="15" Source="{Binding Path=DataGrid}"
    Grid.Row
="1" Width="976" DisplayMode="All" AutoEllipsisMode="Both" NumericButtonCount="10"
    IsTotalItemCountFixed
="True" />
The DataGrid is filled in the ViewModel, then it fires a message that my code-behind listens to.
myDataGrid = new ObservableCollection<Item>();
//Add items to the list
OnPropertyChanged(
"DataGrid");
Messenger.Default.Send(
new NotificationMessage<string>(CurrentFilterString));

Code-behind:
Messenger.Default.Register<NotificationMessage<string>>(this, LoadCustomFilter);
...
private void LoadCustomFilter(NotificationMessage<string> msg)
{
  var serializer =
new DataContractSerializer(typeof(FilterSet), new List<Type> { typeof(Filter) });
  var encoding =
new UTF8Encoding();
  var stream =
new MemoryStream(encoding.GetBytes(msg.Content));
  var fs = (FilterSet)serializer.ReadObject(stream);
  radDataFilter1.FilterDescriptors.Clear();
  radDataFilter1.FilterDescriptors.LogicalOperator = fs.Operator;
  radDataFilter1.FilterDescriptors.AddRange(GetFilterDescriptorsFromFilters(fs.Filters));
}
Where GetFilterDescriptorsFromFilters(fs.Filters) gets a List<IFilter> and returns an LIEnumerable<IFilterDescriptor> and msg.Content is a string of XML style.

I get the InvalidOperationException on the last line of my code-behind (radDataFilter1.FilterDescriptors.AddRange(GetFilterDescriptorsFromFilters(fs.Filters));), saying the the collection has changed.

Any idea how to solve this?
Thanks,
Gen

1 Answer, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 23 Feb 2011, 01:35 PM
Hi Gen,

Unfortunately, we were unable to guess why an InvalidOperationException is thrown. We also don't know what is the cause for this exception, since there is not stack trace and we have nothing that could be debugged. Most probably the FilterDescriptors that are being de-serialized are not ok in some way, but we have no way of telling this.

You will need to isolate the cause for this exception and see why and under what circumstances is it thrown. In other words, if you add a "good" FilterDescriptor to the FilterDescriptors collection there should be no problems. By "good" FilterDescriptor I mean something like:

var fd = new FilterDescriptor("Name", FilterOperator.IsEqualTo, "John");

Save the de-serialized descriptors in a variable and use the debugger to inspect them. Do you notice anything wrong with the. It is possible that something was not serialize/de0serialized correctly, but that is just my guess.

You have to determine when is this exception thrown and what is causing it by looking at the stack trace.
Once you manage to isolate the reason for this exception -- it will be easier to deal with it, since you will know what is actually causing it.

I hope this helps.

Best wishes,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
DataFilter
Asked by
Gen
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Share this question
or