Results 1 to 5 of 5

Thread: Binary Serialization of datagridview and chart tool

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    58

    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

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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 ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    58

    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.

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width