Serialization in Windows Forms Grid Control

21 Aug 20237 minutes to read

GridControl provide the support for serialization and load the serialized grid’s schema information. Serialization is the process of saving the state of an object as a stream of bytes. The reverse of this process is called de-serialization.

GridControl has the different types of serialization techniques as follows,

SOAP - Helps convert the grid schema information to SOAP format.

Binary - Helps convert the grid schema information to binary format.

XML - Helps convert the grid schema information to XML format.

Binary

Binary Serialization is used to convert the grid schema information into Binary format.

Saving Binary

During the binary serialization the schema of the grid will be saved into the file as the bit streams. The SaveBinary method is used to serialize the grid schema into the binary format.

//Binary Serialization
this.gridControl1.SaveBinary("GridSchema");
'Binary Serialization
Me.gridControl1.SaveBinary("GridSchema")

Loading Binary

To load the serialized contents of the encoded grid schema, decode it by using the LoadBinary method and assign it to the grid model.

//Binary Deserialization (Apply the schema to the grid model)
this.gridControl1.Model = GridModel.LoadBinary("GridSchema");
'Binary Deserialization (Apply the schema to the grid model)
Me.gridControl1.Model = GridModel.LoadBinary("GridSchema")

XML

This serialization technique is used to store the data (schema) and Look and feel of the GridControl in to the XML file. This can be applied back to the grid at the time of deserialization.

Serialization

The schema and look and feel of the GridControl can be serialized by using the SaveXml method.

//Serialize the grid schema and Look And Feel to the Xml file
this.gridControl1.SaveXml("GridSchema");
'Serialize the grid schema and Look And Feel to the Xml file
Me.gridControl1.SaveXml("GridSchema")

Deserialization

The InitializeFromXml method is used to deserialize the contents of the XML file to restore the schema and look and feel of the GridControl.

//Deserialize the schema and the look and feel of the grid
this.gridControl1.InitializeFromXml("GridSchema");

this.gridControl1.Refresh();
'Deserialize the schema and the look and feel of the grid
Me.gridControl1.InitializeFromXml("GridSchema")

Me.gridControl1.Refresh()

Saving Grid Data using XML Serialization

The grid data (schema) can alone serialize without the style information (Look and Feel) by using the XmlSerializer method.

//create stream for saving the data
Stream s = File.Create("GridSchema");

//Xml writer for writing the data into the xml format
XmlWriter xmlWriter = new XmlTextWriter(s, System.Text.Encoding.Default);

//Creating the object for serialize the grid data
XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(this.gridControl1.Model.Data.GetType());

//Serialize the content of the grid to xml file
xmlSerializer.Serialize(xmlWriter, this.gridControl1.Model.Data);

s.Close();
'create stream for saving the data
Dim s As Stream = File.Create("GridSchema")

'Xml writer for writing the data into the xml format
Dim xmlWriter As XmlWriter = New XmlTextWriter(s, System.Text.Encoding.Default)

'Creating the object for serializing the grid data
Dim xmlSerializer As XmlSerializer = New System.Xml.Serialization.XmlSerializer(Me.gridControl1.Model.Data.GetType())

'Serialize the content of the grid to xml file
xmlSerializer.Serialize(xmlWriter, Me.gridControl1.Model.Data)

s.Close()

Loading Grid Data using XML Serialization

The schema can be deserialized from the XML file and loaded to the grid by using the following code snippet,

//Open the xml file in the read mode
Stream s = File.OpenRead("GridSchema");

//read the stream of data using the xml reader
XmlReader xmlReader = new XmlTextReader(s);

XmlSerializer xmlSerializer = new XmlSerializer(this.gridControl1.Model.Data.GetType());

//Deserialize the content of xml file to the grid data
this.gridControl1.Model.Data = (GridData)xmlSerializer.Deserialize(xmlReader);

s.Close();

this.gridControl1.Refresh();
'Open the xml file in the read mode
Dim s As Stream = File.OpenRead("GridSchema")

'read the stream of data using the xml reader
Dim xmlReader As XmlReader = New XmlTextReader(s)

Dim xmlSerializer As New XmlSerializer(Me.gridControl1.Model.Data.GetType())

'Deserialize the content of xml file to the grid data
Me.gridControl1.Model.Data = CType(xmlSerializer.Deserialize(xmlReader), GridData)

s.Close()

Me.gridControl1.Refresh()

SOAP

This serialization technique is used to store the schema of the grid in a memory stream rather than the file system.

Saving SOAP

The GridModel.SaveSoap method is used to save the schema of the grid to the memory stream / file.

//Serialize the schema to the memory stream/ file
this.gridControl1.SaveSoap("GridSchemaSoap");

MemoryStream stream = new MemoryStream();

//Save the Grid schema into the memory Stream 
this.gridControl1.SaveSoap(stream);  //this.gridControl1.SaveXml(stream);
'Serialize the schema to the memory stream/ file
Me.gridControl1.SaveSoap("GridSchemaSoap")

Dim stream As New MemoryStream()

'Save the Grid schema into the memory Stream 
Me.gridControl1.SaveSoap(stream) 'this.gridControl1.SaveXml(stream);

Loading SOAP

The GridModel.LoadSoap method is used to deserialize the contents of the file/memory stream to the grid model.

//Load the schema into the Grid model
this.gridControl1.Model = GridModel.LoadSoap("GridSchemaSoap");

//Load the content of the memory stream into the Grid
this.gridControl1.Model = GridModel.LoadSoap(stream);
'Load the schema into the Grid model
Me.gridControl1.Model = GridModel.LoadSoap("GridSchemaSoap")

'Load the content of the memory stream into the Grid
Me.gridControl1.Model = GridModel.LoadSoap(stream)