Serialization and Deserialization in WinForms DataGrid

2 Oct 202315 minutes to read

SfDataGrid allows you to serialize and deserialize the SfDataGrid settings using DataContractSerializer.

Serialization

The SfDataGrid can be serialized by using SfDataGrid.Serialize method which exports the current SfDataGrid control properties to an XML file.

using (var file = File.Create("DataGrid.xml"))
{
    this.sfDataGrid1.Serialize(file);
}
Using file = File.Create("DataGrid.xml")
	Me.sfDataGrid1.Serialize(file)
End Using

Serialize as Stream

The SfDataGrid settings can be stored as Stream using Serialize method by passing the stream.

FileStream stream = new FileStream("DataGrid", FileMode.Create);
this.sfDataGrid1.Serialize(stream);
Dim stream As New FileStream("DataGrid", FileMode.Create)
Me.sfDataGrid1.Serialize(stream)

Serialization Options

SfDataGrid serialization operation can be customized by passing SerializationOptions instance as an argument to Serialize method.

Serialize Sorting

By default, SfDataGrid allows to serialize the sorting operation. The sorting serialization can be disabled by setting the SerializationOptions.SerializeSorting to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeSorting = false;
    this.sfDataGrid1.Serialize(file, options);
}
Using file = File.Create("DataGrid.xml")
	Dim options As New SerializationOptions()
	options.SerializeSorting = False
	Me.sfDataGrid1.Serialize(file, options)
End Using

Serialize Grouping

By default, SfDataGrid allows to serialize the grouping operation. The grouping serialization can be disabled by setting the SerializationOptions.SerializeGrouping to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeGrouping = false;
    this.sfDataGrid1.Serialize(file, options);
}
Using file = File.Create("DataGrid.xml")
	Dim options As New SerializationOptions()
	options.SerializeGrouping = False
	Me.sfDataGrid1.Serialize(file, options)
End Using

Serialize Filtering

By default, SfDataGrid allows to serialize the filtering operation. The filtering serialization can be disabled by setting the SerializationOptions.SerializeFiltering to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeFiltering = false;
    this.sfDataGrid1.Serialize(file, options);
}
Using file = File.Create("DataGrid.xml")
	Dim options As New SerializationOptions()
	options.SerializeFiltering = False
	Me.sfDataGrid1.Serialize(file, options)
End Using

Serialize Columns

By default, SfDataGrid allows to serialize the column manipulation operation. The column manipulation serialization can be disabled by setting the SerializationOptions.SerializeColumn to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeColumns = false;
    this.sfDataGrid1.Serialize(file, options);
}
Using file = File.Create("DataGrid.xml")
	Dim options As New SerializationOptions()
	options.SerializeColumns = False
	Me.sfDataGrid1.Serialize(file, options)
End Using

Serialize Summaries

By default, SfDataGrid allows to serialize the caption summary, group summary and table summary settings in SfDataGrid. The caption summary, group summary and table summary settings serialization can be disabled by setting the SerializationOptions.SerializeCaptionSummaries, SerializationOptions.SerializeTableSummaries and SerializationOptions.SerializeGroupSummaries to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeCaptionSummaries = false;
    options.SerializeTableSummaries = false;
    options.SerializeGroupSummaries = false;
    this.sfDataGrid1.Serialize(file, options);
}
Using file = File.Create("DataGrid.xml")
	Dim options As New SerializationOptions()
	options.SerializeCaptionSummaries = False
	options.SerializeTableSummaries = False
	options.SerializeGroupSummaries = False
	Me.sfDataGrid1.Serialize(file, options)
End Using

Serialize Stacked Headers

By default, SfDataGrid allows to serialize the stacked headers operation. The stacked headers serialization can be disabled by setting the SerializationOptions.SerializeStackedHeaders to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeStackedHeaders = false;
    this.sfDataGrid1.Serialize(file, options);
}
Using file = File.Create("DataGrid.xml")
	Dim options As New SerializationOptions()
	options.SerializeStackedHeaders = False
	Me.sfDataGrid1.Serialize(file, options)
End Using

Serialize Unbound Row

By default, SfDataGrid allows to serialize the unbound rows settings. The unbound rows serialization can be disabled by setting the SerializationOptions.SerializeUnboundRows to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeUnboundRows = false;
    this.sfDataGrid1.Serialize(file, options);
}
Using file = File.Create("DataGrid.xml")
	Dim options As New SerializationOptions()
	options.SerializeUnboundRows = False
	Me.sfDataGrid1.Serialize(file, options)
End Using

Serialize Style

By default, SfDataGrid allows to serialize the style settings. The style serialization can be disabled by setting the SerializationOptions.SerializeStyle to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeStyle = false;
    this.sfDataGrid1.Serialize(file, options);
}
Using file = File.Create("DataGrid.xml")
	Dim options As New SerializationOptions()
	options.SerializeStyle = False
	Me.sfDataGrid1.Serialize(file, options)
End Using

Deserialization

The SfDataGrid can be deserialized by using SfDataGrid.Deserialize method which reconstructs the SfDataGrid based on the setting in the stored XML file.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    this.sfDataGrid1.Deserialize(file);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Me.sfDataGrid1.Deserialize(file)
End Using

Deserialize from Stream

The SfDataGrid settings can be deserialized from Stream using Deserialize method.

FileStream fileStream = new FileStream("DataGrid", FileMode.Open);
this.sfDataGrid1.Deserialize(fileStream);
Dim fileStream As New FileStream("DataGrid", FileMode.Open)
Me.sfDataGrid1.Deserialize(fileStream)

Deserialization Options

Deserialization operation can be customized by passing DeserializationOptions instance as an argument to Deserialize method.

Deserialize Sorting

By default, SfDataGrid allows to deserialize the sorting operation. The sorting deserialization can be disabled by setting the DeserializationOptions.DeserializeSorting to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeSorting = false;
    this.sfDataGrid1.Deserialize(file, options);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Dim options As New DeserializationOptions()
	options.DeserializeSorting = False
	Me.sfDataGrid1.Deserialize(file, options)
End Using

Deserialize Grouping

By default, SfDataGrid allows to deserialize the grouping operation. The grouping deserialization can be disabled by setting the DeserializationOptions.DeserializeGrouping to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeGrouping = false;
    this.sfDataGrid1.Deserialize(file, options);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Dim options As New DeserializationOptions()
	options.DeserializeGrouping = False
	Me.sfDataGrid1.Deserialize(file, options)
End Using

Deserialize Filtering

By default, SfDataGrid allows to deserialize the filtering operation. The filtering deserialization can be disabled by setting the DeserializationOptions.DeserializeFiltering to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeFiltering = false;
    this.sfDataGrid1.Deserialize(file, options);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Dim options As New DeserializationOptions()
	options.DeserializeFiltering = False
	Me.sfDataGrid1.Deserialize(file, options)
End Using

Deserialize Columns

By default, SfDataGrid allows to deserialize the columns manipulation operation. The columns manipulation deserialization can be disabled by setting the DeserializationOptions.DeserializeColumns to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeColumns = false;
    this.sfDataGrid1.Deserialize(file, options);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Dim options As New DeserializationOptions()
	options.DeserializeColumns = False
	Me.sfDataGrid1.Deserialize(file, options)
End Using

Deserialize Summaries

By default, SfDataGrid allows to deserialize the caption summary, group summary and table summary settings in SfDataGrid. The caption summary, group summary and table summary settings deserialization can be disabled by setting the DeserializationOptions.DeserializeCaptionSummary, DeserializationOptions.DeserializeTableSummaries and DeserializationOptions.DeserializeGroupSummaries to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeCaptionSummary = false;
    options.DeserializeGroupSummaries = false;
    options.DeserializeTableSummaries = false;
    this.sfDataGrid1.Deserialize(file, options);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Dim options As New DeserializationOptions()
	options.DeserializeCaptionSummary = False
	options.DeserializeGroupSummaries = False
	options.DeserializeTableSummaries = False
	Me.sfDataGrid1.Deserialize(file, options)
End Using

Deserialize Stacked Headers

By default, SfDataGrid allows to deserialize the stacked headers operation. The stacked headers deserialization can be disabled by setting the DeserializationOptions.DeserializeStackedHeaders to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeStackedHeaders = false;
    this.sfDataGrid1.Deserialize(file, options);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Dim options As New DeserializationOptions()
	options.DeserializeStackedHeaders = False
	Me.sfDataGrid1.Deserialize(file, options)
End Using

Deserialize Unbound Row

By default, SfDataGrid allows to deserialize the unbound rows settings. The unbound rows deserialization can be disabled by setting the DeserializationOptions.DeserializeUnboundRows to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeUnboundRows = false;
    this.sfDataGrid1.Deserialize(file, options);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Dim options As New DeserializationOptions()
	options.DeserializeUnboundRows = False
	Me.sfDataGrid1.Deserialize(file, options)
End Using

Deserialize Style

By default, SfDataGrid allows to deserialize the style settings. The style deserialization can be disabled by setting the DeserializationOptions.DeserializeStyle to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeStyle = false;
    this.sfDataGrid1.Deserialize(file, options);
}
Using file = File.Open("DataGrid.xml", FileMode.Open)
	Dim options As New DeserializationOptions()
	options.DeserializeStyle = False
	Me.sfDataGrid1.Deserialize(file, options)
End Using