Serializing DataTable with Data Injected from DataGridView
I am wondering if it is possible to add data to the DataTable via the DataGridView and store the data to the DataTable via serialization - all without having to create an underlying database. One DataTable is sufficient for the amount of data I am trying to save, and the database is definitely full.
Thanks!
I don't think I understand you on the issue of serialization.
If you assign the DataTable to the DataGridView property of the DataGridView when you enter data on the form, it will be automatically added to the DataTable.
If you want to store data inside a DataTable outside the database, you must use the DataTable's WriteXml () method (and ReadXml () to load the data). As you can see, the data is serialized in xml format.
a source to share
Not only possible, but also quite simple. I am only going with dataset containing data that datagridviews use for input. Dgv uses bind sources as datasource and bindings source has dataset as datasource and datatable as datamember.
I am serializing a dataset in a varbinary (max) column in SQL Server.
(I am using strongly typed properties on my business object, but that should give you an idea)
'-- Copy dataset to property
'-- Establish locals
Dim loFormatter As New BinaryFormatter()
Dim loStream As New System.IO.MemoryStream()
'-- Serialize the business object
loFormatter.Serialize(loStream, Me.DsPolicies1)
'-- Return the created stream
Me.PoliciesBO1.Dataset_Bytes = loStream.ToArray()
When the nav entry is moved, the property is deserialized
If Me.PoliciesBO1.Count > 0 And Me.PoliciesBO1.CurrentRowIndex >= 0 Then
Me.clear_bindingsources()
Dim loformatter As New BinaryFormatter()
Dim lomemorystream As MemoryStream = _
New MemoryStream(Me.PoliciesBO1.Dataset_Bytes, 0, _
Me.PoliciesBO1.Dataset_Bytes.Length, True)
Me.DsPolicies1 = _
CType(loformatter.Deserialize(lomemorystream), dsPolicies)
'-- Rehook datasource
Me.rehook_Bindingsources()
Me.refresh_dgvs()
The last two subdirectories only reset the bindingsource and datasource for each table, and then update each dgv
(I use them to collect data to fill out PDF forms)
The only tricky part is remembering that the datasource must be empty in the new record and must be completely reloaded from the data after moving the parent pointer.
a source to share