Binary Serialization of datagridview and chart tool
Hey,
I am trying to use binary serialization to store data from a few textboxes, a few datagridviews and a chart with multiple series, but havent had much luck.
My current problem is that i'm trying to adapt an example to work with DGV's and I need to declare it. I have been trying to follow the format of the example.
Code:
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
<Serializable()> _
Public Class bob
Implements ISerializable
Public Title As String = String.Empty
Public Author As String = String.Empty
Public Text As String = String.Empty
Public texttest As String = String.Empty
Public datagrid As DataGridView = 'find a way to store datagridviews
Public Sub GetObjectData(ByVal info As SerializationInfo, _
ByVal context As StreamingContext) _
Implements ISerializable.GetObjectData
info.AddValue("Title", Title)
info.AddValue("Author", Author)
info.AddValue("Text", Text)
info.AddValue("TextTest", texttest)
info.AddValue("datagrid", datagrid)
End Sub
Public Sub New(ByVal info As SerializationInfo, _
ByVal context As StreamingContext)
Title = CStr(info.GetValue("Title", GetType(String)))
Author = CStr(info.GetValue("Author", GetType(String)))
Text = CStr(info.GetValue("Text", GetType(String)))
texttest = CStr(info.GetValue("TextTest", GetType(String)))
datagrid = (info.GetValue("datagrid", GetType(String)))
End Sub
Public Sub New()
'Empty Constructor
End Sub
Public Sub Serialize(ByVal Filename As String)
Dim s As Stream
Try
s = File.Open(Filename, FileMode.Create, FileAccess.ReadWrite)
Dim b As New BinaryFormatter
b.Serialize(s, Me)
Finally
s.Close()
End Try
End Sub
Public Shared Function Deserialize(ByVal Filename As String) As bob
Dim s As Stream
Try
s = File.Open(Filename, FileMode.Open, FileAccess.Read)
Dim b As New BinaryFormatter
Return CType(b.Deserialize(s), bob)
Finally
s.Close()
End Try
End Function
End Class
Not sure if its possible the way that i'm doing it, is there a better way? Possibly a way to serialize the entire program at once, and save it in its current state? That would be ideal.
Thanks :)
Re: Binary Serialization of datagridview and chart tool
Wait a minute.....where does the data in the DataGridView come from ? Data in controls should be considered disposable. Ie. They are for display purposes only and you should practice thinking in that way. What you should be doing is using the source of that data(before it reached the DataGridView) to save, edit, delete etc. You understand what I'm saying ?
Re: Binary Serialization of datagridview and chart tool
Completely agree with Niya. Here's a nice simple example showing a BindingList(Of T) being serialized/deserialized while having the list be the DataGridView's DataSource.
Re: Binary Serialization of datagridview and chart tool
I never thought of it that way. Currently the data is calculated, then added to the datagridview and then looped. I will look into Binding list(of T). Thank You both :)
Any ideas for the chart tool? Each series is generated from the data in the datagrids, if that helps.
Re: Binary Serialization of datagridview and chart tool
Here's the msdn documentation for serializing a chart. Pay attention to the section that talks about setting the Format to SerializationFormat.Binary as XML is the default.