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

Exception when serializing a gridview which has filters

10 Answers 120 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Julien
Top achievements
Rank 1
Julien asked on 21 Mar 2011, 01:18 PM
Hi,

I'm trying to save several RadPanes, which are owning gridviews:

//radPanes is a IEnumerable<RadPane>
MemoryStream ms =
new MemoryStream();
object[] objects = new List<object>(radPanes.Select(DocumentSerialization.GetDocumentSerialization).ToArray()) { new DockingSerialization(docking) }.ToArray();
serializer.WriteObject(ms,objects);
byte[] bytes = ms.ToArray();
ms.Close();

And then I get this exception:
System.Runtime.Serialization.InvalidDataContractException: Type 'Telerik.Windows.Data.FilterDescriptor+FilterDescriptorUnsetValue' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.


How can I avoid this?

10 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 21 Mar 2011, 01:24 PM
Hi Julien,

May you take a look at this forum thread for a reference ?
 

Kind regards,
Maya
the Telerik team
0
Julien
Top achievements
Rank 1
answered on 21 Mar 2011, 02:01 PM
So it's a bug? Not resolved since the 6 oct.?

so... Okay but:

I can't many any change on object of the window because users continue to use after the serialization of properties.

How do I obtain your cfd variable?

Here what I tried, but I don't know what I have to do in my "if", because, I don't want to modify the filterSetting of my user control
foreach (ListSerialization objectToSerialize in objects.OfType<ListSerialization>()    )
{
    if (objectToSerialize.GridSettings.ContainsKey("FilterSettings") && objectToSerialize.GridSettings["FilterSettings"] is List<FilterSetting> && ((List<FilterSetting>)objectToSerialize.GridSettings["FilterSettings"]).Count>0)
    {
        List<FilterSetting> filterSettings = ((List<FilterSetting>) objectToSerialize.GridSettings["FilterSettings"]);
        foreach (FilterSetting filterSetting in filterSettings)
        {
            if(filterSetting.Filter1 == FilterDescriptor.UnsetValue)
            {
                 
            }
            if(filterSetting.Filter2==FilterDescriptor.UnsetValue)
            {
                 
            }
        }
    }
}
0
Vlad
Telerik team
answered on 21 Mar 2011, 02:47 PM
0
Julien
Top achievements
Rank 1
answered on 21 Mar 2011, 03:24 PM
With this, I can Serialize without exceptions:
MemoryStream ms = new MemoryStream();
object[] objects = new List<object>(radPanes.Select(DocumentSerialization.GetDocumentSerialization).ToArray()) { new DockingSerialization(docking) }.ToArray();
foreach (ListSerialization objectToSerialize in objects.OfType<ListSerialization>()    )
{
    if (objectToSerialize.GridSettings.ContainsKey("FilterSettings") && objectToSerialize.GridSettings["FilterSettings"] is List<FilterSetting> && ((List<FilterSetting>)objectToSerialize.GridSettings["FilterSettings"]).Count>0)
    {
        List<FilterSetting> filterSettings = ((List<FilterSetting>) objectToSerialize.GridSettings["FilterSettings"]);
        foreach (FilterSetting filterSetting in filterSettings)
        {
            if(filterSetting.Filter1.Value == FilterDescriptor.UnsetValue)
            {
                filterSetting.Filter1.Value = null;
 
            }
            if (filterSetting.Filter2.Value == FilterDescriptor.UnsetValue)
            {
                filterSetting.Filter2.Value = null;
            }
        }
    }
}
serializer.WriteObject(ms,objects);

But now when I deserialize: I got this exception:
The type System.Windows.DependencyObject "can not be deserialized because it hasno public parameterless constructor. You can also make and use the internalInternalsVisibleToAttribute attribute on your assembly to allow serialization of internal members. Refer to the documentation for more details. Be aware that this operation hassome implications for safety.

I do it like this: 
DataContractSerializer serializer = GetSerializer();
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(layout.SerializedData));
 [...] =(object[])serializer.ReadObject(ms);

The serialization/deserialization is working for group/sort/... everything except fitler.

What is wrong?

Thank you for your help

EDIT: I didn't saw your previous post, like I said, I'm serializing multiple RadPane which contains RadGridView, so we have a method which return us all settings available:
public static DocumentSerialization GetDocumentSerialization(ExtendRadPane pane) {
            DocumentSerialization documentSerialization;
            if(pane.Document is IDetailsDocument) {
                documentSerialization = new DetailsSerialization { ElementId = ((IDetailsDocument)pane.Document).ElementId };
            } else if(pane.Document is IFilterDocument) {
                FilterSerialization serialization = new FilterSerialization {
                    GridSettings = ListSerialization.GetGridSettings(((IFilterDocument)pane.Document).Grid),
                    Filter = ((IFilterDocument)pane.Document).Filter
                };
                documentSerialization = serialization;
            } else if(pane.Document is IListDocument) {
                documentSerialization = new ListSerialization {
                    GridSettings = ListSerialization.GetGridSettings(((IListDocument)pane.Document).Grid)
                };
            } else {
                documentSerialization = new DocumentSerialization();
            }
            documentSerialization.DockType = pane.DockType;
            documentSerialization.Key = pane.Key;
            return documentSerialization;
        }
0
Julien
Top achievements
Rank 1
answered on 21 Mar 2011, 03:56 PM
And here is how we load/save our settings:
public static void LoadState(RadGridViewApplicationSettings settings, RadGridView grid)
       {
           if (grid == null)
           {
               return;
           }
           grid.FrozenColumnCount = settings.FrozenColumnCount;
 
           if (settings.ColumnSettings.Count > 0)
           {
               foreach (ColumnSetting setting in settings.ColumnSettings)
               {
                   ColumnSetting currentSetting = setting;
 
                   GridViewDataColumn column = (from c in grid.Columns.OfType<GridViewDataColumn>()
                                                where c.UniqueName == currentSetting.UniqueName
                                                select c).FirstOrDefault();
 
                   if (column != null)
                   {
                       if (currentSetting.DisplayIndex != null && currentSetting.DisplayIndex.Value < grid.Columns.Count && currentSetting.DisplayIndex.Value != -1)
                       {
                           column.DisplayIndex = currentSetting.DisplayIndex.Value;
                       }
 
                       if (setting.Width != null && setting.Width.Value > 90)
                       {
                           column.Width = new GridViewLength(setting.Width.Value);
                       }
                   }
               }
           }
           using (grid.DeferRefresh())
           {
               if (settings.SortSettings.Count > 0)
               {
                   grid.SortDescriptors.Clear();
 
                   foreach (SortSetting setting in settings.SortSettings)
                   {
                       SortDescriptor d = new SortDescriptor { Member = setting.PropertyName, SortDirection = setting.SortDirection };
 
                       grid.SortDescriptors.Add(d);
                   }
               }
 
               if (settings.GroupSettings.Count > 0)
               {
                   grid.GroupDescriptors.Clear();
 
                   foreach (GroupSetting setting in settings.GroupSettings)
                   {
                       GroupDescriptor d = new GroupDescriptor { Member = setting.PropertyName, SortDirection = setting.SortDirection, DisplayContent = setting.DisplayContent };
 
                       grid.GroupDescriptors.Add(d);
                   }
               }
 
               if (settings.FilterSettings.Count > 0)
               {
                   foreach (FilterSetting setting in settings.FilterSettings)
                   {
                       FilterSetting currentSetting = setting;
 
                       GridViewDataColumn matchingColumn = (from column in grid.Columns.OfType<GridViewDataColumn>()
                                                            where column.DataMemberBinding.Path.Path == currentSetting.PropertyName
                                                            select column).FirstOrDefault();
 
                       if (matchingColumn != null)
                       {
                           ColumnFilterDescriptor cfd = new ColumnFilterDescriptor(matchingColumn);
                           if (setting.Filter1 != null)
                           {
                               cfd.FieldFilter.Filter1.Member = setting.Filter1.Member;
                               cfd.FieldFilter.Filter1.Operator = setting.Filter1.Operator;
                               cfd.FieldFilter.Filter1.Value = setting.Filter1.Value;
                           }
                           if (setting.Filter2 != null)
                           {
                               cfd.FieldFilter.Filter2.Member = setting.Filter2.Member;
                               cfd.FieldFilter.Filter2.Operator = setting.Filter2.Operator;
                               cfd.FieldFilter.Filter2.Value = setting.Filter2.Value;
                           }
 
                           foreach (FilterDescriptor descriptor in setting.SelectedDistinctValues)
                           {
                               FilterDescriptor newDescriptor = new FilterDescriptor();
                               newDescriptor.Member = descriptor.Member;
                               newDescriptor.Operator = descriptor.Operator;
                               newDescriptor.Value = descriptor.Value;
 
                               cfd.DistinctFilter.FilterDescriptors.Add(newDescriptor);
                           }
 
                           grid.FilterDescriptors.Add(cfd);
                       }
                   }
               }
           }
       }
 
       /// <summary>
       /// Saves the state.
       /// </summary>
       /// <param name="grid">The grid.</param>
       /// <returns></returns>
       public static RadGridViewApplicationSettings SaveState(RadGridView grid)
       {
           RadGridViewApplicationSettings settings = new RadGridViewApplicationSettings();
           if (grid != null)
           {
               if (grid.Columns != null)
               {
                   settings.ColumnSettings.Clear();
 
                   foreach (GridViewColumn column in grid.Columns)
                   {
                       if (column is GridViewDataColumn)
                       {
                           GridViewDataColumn dataColumn = (GridViewDataColumn)column;
 
                           ColumnSetting setting = new ColumnSetting
                           {
                               PropertyName = dataColumn.DataMemberBinding.Path.Path,
                               UniqueName = dataColumn.UniqueName,
                               Header = dataColumn.Header,
                               Width = dataColumn.ActualWidth,
                               DisplayIndex = dataColumn.DisplayIndex
                           };
 
                           settings.ColumnSettings.Add(setting);
                       }
                   }
               }
 
               if (grid.FilterDescriptors != null)
               {
                   settings.FilterSettings.Clear();
 
                   foreach (IColumnFilterDescriptor cfd in grid.FilterDescriptors.OfType<IColumnFilterDescriptor>())
                   {
                       FilterSetting setting = new FilterSetting();
                       if (cfd.FieldFilter.Filter1.Value != Telerik.Windows.Data.FilterDescriptor.UnsetValue)
                           setting.Filter1 = new FilterDescriptor
                           {
                               Member = cfd.FieldFilter.Filter1.Member,
                               Operator = cfd.FieldFilter.Filter1.Operator,
                               Value = cfd.FieldFilter.Filter1.Value,
                               MemberType = null
                           };
                       if (cfd.FieldFilter.Filter2.Value != Telerik.Windows.Data.FilterDescriptor.UnsetValue)
                           setting.Filter2 = new FilterDescriptor
                           {
                               Member = cfd.FieldFilter.Filter2.Member,
                               Operator = cfd.FieldFilter.Filter2.Operator,
                               Value = cfd.FieldFilter.Filter2.Value,
                               MemberType = null
                           };
 
                       foreach (FilterDescriptor fd in cfd.DistinctFilter.FilterDescriptors.OfType<FilterDescriptor>())
                       {
                           if (fd.Value == Telerik.Windows.Data.FilterDescriptor.UnsetValue)
                               continue;
                           FilterDescriptor newFd = new FilterDescriptor { Member = fd.Member, Operator = fd.Operator, Value = fd.Value, MemberType = null };
                           setting.SelectedDistinctValues.Add(newFd);
                       }
 
                       setting.PropertyName = cfd.Column.DataMemberBinding.Path.Path;
 
                       settings.FilterSettings.Add(setting);
                   }
               }
 
               if (grid.SortDescriptors != null)
               {
                   settings.SortSettings.Clear();
 
                   foreach (SortDescriptor d in grid.SortDescriptors.OfType<SortDescriptor>())
                   {
                       SortSetting setting = new SortSetting { PropertyName = d.Member, SortDirection = d.SortDirection };
 
                       settings.SortSettings.Add(setting);
                   }
 
                   foreach (ColumnSortDescriptor d in grid.SortDescriptors.OfType<ColumnSortDescriptor>())
                   {
                       string member = (d.Column as GridViewDataColumn).GetDataMemberName();
                       SortSetting setting = new SortSetting { PropertyName = member, SortDirection = d.SortDirection };
 
                       settings.SortSettings.Add(setting);
                   }
               }
 
               if (grid.GroupDescriptors != null)
               {
                   settings.GroupSettings.Clear();
 
                   foreach (GroupDescriptor d in grid.GroupDescriptors.OfType<GroupDescriptor>())
                   {
                       GroupSetting setting = new GroupSetting { PropertyName = d.Member, SortDirection = d.SortDirection, DisplayContent = d.DisplayContent };
 
                       settings.GroupSettings.Add(setting);
                   }
 
                   foreach (ColumnGroupDescriptor d in grid.GroupDescriptors.OfType<ColumnGroupDescriptor>())
                   {
                       string member = (d.Column as GridViewDataColumn).GetDataMemberName();
                       GroupSetting setting = new GroupSetting { PropertyName = member, SortDirection = d.SortDirection, DisplayContent = d.DisplayContent };
 
                       settings.GroupSettings.Add(setting);
                   }
               }
 
               settings.FrozenColumnCount = grid.FrozenColumnCount;
           }
           return settings;
       }
0
Julien
Top achievements
Rank 1
answered on 22 Mar 2011, 08:13 AM
And here is what is serialized  for the gridview:

I don't see anything about a binding into this ? If I have a serialization file with <Value i:type="d2p1:ArrayOfFilterSetting"/> it works.
<d2p1:GridSettings>
  <KeyValueOfstringanyType>
    <Key>ColumnSettings</Key>
    <Value i:type="d2p1:ArrayOfColumnSetting">
      <d2p1:ColumnSetting>
        <d2p1:PropertyName>Name</d2p1:PropertyName>
        <d2p1:DisplayIndex>0</d2p1:DisplayIndex>
        <d2p1:Header xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:string">Nom</d2p1:Header>
        <d2p1:UniqueName>Name</d2p1:UniqueName>
        <d2p1:Width>320</d2p1:Width>
      </d2p1:ColumnSetting>
      <d2p1:ColumnSetting>
        <d2p1:PropertyName>ShortName</d2p1:PropertyName>
        <d2p1:DisplayIndex>1</d2p1:DisplayIndex>
        <d2p1:Header xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:string">Nom court</d2p1:Header>
        <d2p1:UniqueName>ShortName</d2p1:UniqueName>
        <d2p1:Width>89</d2p1:Width>
      </d2p1:ColumnSetting>
      <d2p1:ColumnSetting>
        <d2p1:PropertyName>Program.Name</d2p1:PropertyName>
        <d2p1:DisplayIndex>2</d2p1:DisplayIndex>
        <d2p1:Header xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:string">Programme</d2p1:Header>
        <d2p1:UniqueName>Program.Name</d2p1:UniqueName>
        <d2p1:Width>257</d2p1:Width>
      </d2p1:ColumnSetting>
    </Value>
  </KeyValueOfstringanyType>
  <KeyValueOfstringanyType>
    <Key>FilterSettings</Key>
    <Value i:type="d2p1:ArrayOfFilterSetting">
      <d2p1:FilterSetting>
        <d2p1:PropertyName>Name</d2p1:PropertyName>
        <d2p1:Filter1 xmlns:d7p1="http://schemas.datacontract.org/2004/07/Telerik.Windows.Data" i:nil="true" />
        <d2p1:Filter2 xmlns:d7p1="http://schemas.datacontract.org/2004/07/Telerik.Windows.Data" i:nil="true" />
        <d2p1:SelectedDistinctValues xmlns:d7p1="http://schemas.datacontract.org/2004/07/Telerik.Windows.Data">
          <d7p1:FilterDescriptor>
            <d7p1:IsCaseSensitive>false</d7p1:IsCaseSensitive>
            <d7p1:Member>Name</d7p1:Member>
            <d7p1:MemberType xmlns:d9p1="http://schemas.datacontract.org/2004/07/System" i:nil="true" />
            <d7p1:Operator>IsEqualTo</d7p1:Operator>
            <d7p1:Value xmlns:d9p1="http://www.w3.org/2001/XMLSchema" i:type="d9p1:string">My Filter value</d7p1:Value>
          </d7p1:FilterDescriptor>
        </d2p1:SelectedDistinctValues>
      </d2p1:FilterSetting>
    </Value>
  </KeyValueOfstringanyType>
  <KeyValueOfstringanyType>
    <Key>SortSettings</Key>
    <Value i:type="d2p1:ArrayOfSortSetting" />
  </KeyValueOfstringanyType>
  <KeyValueOfstringanyType>
    <Key>GroupSettings</Key>
    <Value i:type="d2p1:ArrayOfGroupSetting" />
  </KeyValueOfstringanyType>
  <KeyValueOfstringanyType>
    <Key>FrozenColumnCount</Key>
    <Value xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:int">0</Value>
  </KeyValueOfstringanyType>
</d2p1:GridSettings>
0
Vlad
Telerik team
answered on 22 Mar 2011, 08:28 AM
Hello,

 Have you checked the latest code in the demo I've posted in my previous reply?

Regards,
Vlad
the Telerik team
0
Julien
Top achievements
Rank 1
answered on 22 Mar 2011, 08:29 AM
Yes, and like you can see our RadGridView setting does the same thing than in your example
0
Accepted
Vlad
Telerik team
answered on 22 Mar 2011, 08:34 AM
Hi,

 It seems that you've missed the new code for serializing filter? Here is it:

if (grid.FilterDescriptors != null)
                {
                    Settings.FilterSettings.Clear();


                    foreach (IColumnFilterDescriptor cfd in grid.FilterDescriptors.OfType<IColumnFilterDescriptor>())
                    {
                        FilterSetting setting = new FilterSetting();


                        if (cfd.FieldFilter.Filter1.Value != Telerik.Windows.Data.FilterDescriptor.UnsetValue)
                        {
                            setting.Filter1 = new FilterDescriptorSetting();
                            setting.Filter1.Member = cfd.FieldFilter.Filter1.Member;
                            setting.Filter1.Operator = cfd.FieldFilter.Filter1.Operator;
                            setting.Filter1.Value = cfd.FieldFilter.Filter1.Value;
                        }


                        if (cfd.FieldFilter.Filter2.Value != Telerik.Windows.Data.FilterDescriptor.UnsetValue)
                        {
                            setting.Filter2 = new FilterDescriptorSetting();
                            setting.Filter2.Member = cfd.FieldFilter.Filter2.Member;
                            setting.Filter2.Operator = cfd.FieldFilter.Filter2.Operator;
                            setting.Filter2.Value = cfd.FieldFilter.Filter2.Value;
                        }


                        foreach (Telerik.Windows.Data.FilterDescriptor fd in cfd.DistinctFilter.FilterDescriptors.OfType<Telerik.Windows.Data.FilterDescriptor>())
                        {
                            if (fd.Value == Telerik.Windows.Data.FilterDescriptor.UnsetValue)
                            {
                                continue;
                            }


                            FilterDescriptorSetting fds = new FilterDescriptorSetting();
                            fds.Member = fd.Member;
                            fds.Operator = fd.Operator;
                            fds.Value = fd.Value;
                            setting.SelectedDistinctValues.Add(fds);
                        }


                        setting.PropertyName = cfd.Column.DataMemberBinding.Path.Path;


                        Settings.FilterSettings.Add(setting);
                    }
                }

Regards,
Vlad
the Telerik team
0
Julien
Top achievements
Rank 1
answered on 22 Mar 2011, 08:58 AM
Thank you! I didn't noticed the change.

Julien
Tags
GridView
Asked by
Julien
Top achievements
Rank 1
Answers by
Maya
Telerik team
Julien
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or