Data serialization is a process of saving user preferences for the grid, including colors, columns location, grouping, sorting and other data into an archive for further deserialization. For example, this feature may be useful when an application is restarted. In general, this procedure is very complex and requires familiarity with XML or binary archives. Besides that, a programmer sometimes fails to save overall application state, which results in loss of data and user preferences.

We provide a very convenient and efficient way to save and restore the grid's state. The .Net Grid supports serialization in XML and binary formats via the Grid.SerializationState property. The object returned by this property implements IXmlSerializable and ISerializable interfaces which allow it to be serialized into XML or binary files. During deserialization this object is restored from these files and is passed to the grid via the Grid.SerializationState property.

Example of XML serialization:

C# Copy imageCopy
//The method serializes the grid's state into the XML file
void XmlSerialization(Grid grid, string fileName)
{
    using(FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Grid.GridSerializationState));
        serializer.Serialize(fs, grid.SerializationState);    
    }
}

//The method deserializes the grid's state from the XML file
void XmlDeserialization(Grid grid, string fileName)
{
    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Grid.GridSerializationState));
        grid.SerializationState = (Grid.GridSerializationState) serializer.Deserialize(fs);
    }
}

Example of binary serialization:

C# Copy imageCopy
//The method serializes the grid's state into the XML file
void BinarySerialization(Grid grid, string fileName)
{
    using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(fs, grid.SerializationState);
    }
}

//The method deserializes the grid's state from the XML file
void BinaryDeserialization(Grid grid, string fileName)
{
    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
        BinaryFormatter formatter = new BinaryFormatter();
        grid.SerializationState = (Grid.GridSerializationState) formatter.Deserialize(fs); 
    }
}

A very nice feature of the Dapfor.Net Grid is that a programmer may transfer state of one grid into another:

C# Copy imageCopy
grid1.SerializationState = grid2.SerializationState;

Here is an example that shows how to serialize multiple grids to an xml file.