Home / Community & Support / Knowledge Base / RadControls for WinForms / GridView / Utilizing RadGridView's Serialization API

Utilizing RadGridView's Serialization API

Article Info

Rating: 5

Article information

Article relates to

 RadGridView for WinForms, Q2 2010

Created by

 Deyan Ginev

Last modified

July 20, 2010

Last modified by

 Alexander Georgiev, Telerik


HOW-TO
Use the RadGridView XML Serialization API in order to customize how property values should be persisted and later restored.

DESCRIPTION
The RadGridView control provides a convenient serialization API which enables the programmer to fine tune how layout and other appearance settings should be persisted in XML format and later restored, so that end-user settings do not get lost between the states of the application. Besides the default serialization behavior, there are plenty of options to explore and use, so that the amount of the serialization data is decreased and the performance is optimized.

In many cases the programmer would like to avoid serializing some of the properties of the RadGridView which are not important in the current application context, but yet are serialized. We have prepared an example which aims to demonstrate how to alter the default serialization procedure of the grid and customize what will be persisted and what will be not.

SOLUTION
In this article I am going to instruct the RadGridView to only serialize the width settings of its columns by using the XmlSerializationInfo property which is used to access the settings of the serialization engine.

Let's begin with the following propery:

this.radGridView1.XmlSerializationInfo.DisregardOriginalSerializationVisibility = true;

The DisregardOriginalSerializationVisibility property determines whether the serialization engine will take into account the default serialization attributes of each of the RadGridView's properties when persisting data, or it will read the custom serialization metadata provided by the programmer.

this.radGridView1.XmlSerializationInfo.SerializationMetadata.Clear();

The SerializationMetadata property returns a collection of PropertySerializationMetadata objects which contain information about the type, the name and the serialization method which is used when serializing the corresponding property. We clear the collection because we want to be sure that only the serialization instructions will be considered. When the DisregardOriginalSerializationVisibility is set to true, the objects in this collection are used by the serialization engine in order to obtain information about what will be persisted. The tricky moment here is to properly instruct the serialization engine how to read properties which contain objects that cannot be serialized in a straightforward manner.

this.radGridView1.XmlSerializationInfo.SerializationMetadata.Add(
    typeof(RadGridView),
    "MasterTemplate",
    DesignerSerializationVisibilityAttribute.Content);

The MasterTemplate property contains the data and its structure which is to be shown in the RadGridView. With the above code, I tell the serialization engine that I want the MasterTemplate property, which type is GridViewTemplate and is part of the RadGridView class, to be taken into account when serializing. The DesignerSerializationVisibilityAttribute works exactly as the standard Microsoft .NET Framework attribute works.

Now take a look at the following code snippet:

this.radGridView1.XmlSerializationInfo.SerializationMetadata.Add(
    typeof(GridViewTemplate), "Columns", DesignerSerializationVisibilityAttribute.Content);

We will skip the “ChildTemplates line for now and continue with the Columns property of the GridViewTemplate.

Since the Columns property points to a complex object as well, we specify the Content attribute for it. In this case, the serialization engine will automatically detect that Columns points to a collection and will enumerate its items.

Now, for each column we should define that we want its UniqueName stored in the XML output. Take a look at the part of the code that is responsible for this:

this.radGridView1.XmlSerializationInfo.SerializationMetadata.Add(
    typeof(GridViewDataColumn), "UniqueName",
    DesignerSerializationVisibilityAttribute.Visible);

By specifying the GridViewDataColumn type we easily tell the serializer that each time an object of this type is found, this instruction should be considered. In this way, all columns in the collection previously mentioned, will be matched with this instruction. As you also can see, we use the Visibility attribute in order to specify that the object that this property points to is the value that we want to serialize as well.

At the end, we have to instruct the serialization engine that it should persist the Width of each column:

this.radGridView1.XmlSerializationInfo.SerializationMetadata.Add(
    typeof(GridViewDataColumn), "Width",
    DesignerSerializationVisibilityAttribute.Visible);

Since this property returns the exact value we want to store, we once again use the Visibility attribute.

You can also take a look at the attached demo application and its source code. We have chosen to implement two cases in order to compare both the default and custom serialization options of the RadGridView. You can switch between these two cases by using the corresponding radio buttons in the UI of the application.

SUMMARY
The RadGridView’s Serialization API provides a wide range of options that enable the programmer to customize the serialized content in a flexible manner. Based on the principles of the standard Microsoft .NET XML serialization approach, this API can be easily used for creating different serialization scenarios and thus enabling the end user with new options to explore when saving the RadGridView’s layout.


Comments

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.