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.
.radGridView1.XmlSerializationInfo.SerializationMetadata.Clear();
.radGridView1.XmlSerializationInfo.SerializationMetadata.Add(
typeof
(RadGridView),
"MasterTemplate"
,
DesignerSerializationVisibilityAttribute.Content);
Now take a look at the following code snippet:
(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);
(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.
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.
"Width"
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.
Resources Buy Try