我正在尝试创建一个XML文件来保存项目数据。从1 DGV输入数据,但当我尝试将另一个表添加到数据集时,第一个表被覆盖,我得到的只是XML文件中第二个DGV的表列标题。所以我想知道如何在一个XML文件中从多个DGV获取数据?还需要将一些文本框数据添加到同一文件中。这是我目前使用的代码:
Public Class Dimensioneringsapp
'data sets voor het opslaan en openen van data
Dim path As String
Dim SaveData As DataSet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SaveData = CreateDatasetTable(Rekenwaardes, "Reken")
SaveData = CreateDatasetTable(XYinput, "input")
End Sub
'load
Private Sub OpenProjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenProjectToolStripMenuItem.Click
If opendialog.ShowDialog = Windows.Forms.DialogResult.OK Then
path = opendialog.FileName
'voer het bestand in de tabellen
LoadFromXMLfile(path)
End If
End Sub
'save
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
path = saveFileDialog1.FileName
SaveData.Clear()
AddDataToDataSet(SaveData, Rekenwaardes, "reken")
AddDataToDataSet(SaveData, XYinput, "input")
SaveToXMLFile(path, SaveData)
End If
End Sub
'Create Data set
Private Function CreateDatasetTable(g As DataGridView, t As String) As DataSet
Dim dataset1 As New DataSet("SaveData")
Dim table1 As New DataTable(t)
'counter
Dim i As Integer = 0
Do While i < g.ColumnCount
table1.Columns.Add(g.Columns(i).HeaderText)
i = i + 1
Loop
dataset1.Tables.Add(table1)
Return dataset1
End Function
Private Sub AddDataToDataSet(d As DataSet, g As DataGridView, t As String)
'counter voor doorlopen tabel
Dim i As Integer = 0
Dim c As Integer = 0
Do While i < g.RowCount
Do While c < g.ColumnCount
d.Tables(t).Rows.Add(g(c, i).Value)
c = c + 1
Loop
i = i + 1
Loop
End Sub
Private Sub SaveToXMLFile(filename As String, d As DataSet)
'd.WriteXml(filename, XmlWriteMode.WriteSchema)
Dim ser As XmlSerializer = New XmlSerializer(GetType(DataSet))
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, d)
writer.Close()
End Sub
Private Sub LoadFromXMLfile(filename As String)
If System.IO.File.Exists(filename) Then
Dim xmlSerializer As XmlSerializer = New XmlSerializer(SaveData.GetType)
Dim readStream As FileStream = New FileStream(filename, FileMode.Open)
Rekenwaardes.Columns.Clear()
SaveData = CType(xmlSerializer.Deserialize(readStream), DataSet)
readStream.Close()
'SaveData.ReadXml(filename, XmlReadMode.ReadSchema)
Rekenwaardes.DataSource = SaveData.Tables("Reken")
XYinput.DataSource = SaveData.Tables("input")
End If
End Sub
End Class
答案 0 :(得分:0)
我会使用代表您要保存的数据的自定义对象。因此,我将创建一个类型,其中包含文本数据的字符串属性,以及数据集的数组(或List或IEnumerable)。然后我会序列化该对象。我会使用JSON,但序列化的方法取决于你。
Public Class ProjectData
Public Property Text As String
Public Property DataSets As New List(Of DataSet)
End Class
答案 1 :(得分:0)
如果在Visual Studio中使用DataSet设计器来定义表,它将创建一个“强类型”DataSet。也就是说,一个继承自DataSet但已经为您定义的表模式的类。这为您提供了其他优点,例如能够访问dataSet.TableName而不是dataSet.Table(“TableName”)。
如果您对XML的格式不是很挑剔,那么您可以使用DataSet上的ReadXml和WriteXml方法来序列化整个事物。
然后在DataGridViews上使用BindingSource
Dim bs1 As New BindingSource
bs1.DataSource = myDataSet
bs1.DataMember = "Table1"
dgv1.DataSource = bs1
Dim bs2 As New BindingSource
bs2.DataSource = myDataSet
bs2.DataMember = "Table2"
dgv2.DataSource = bs2