0

我有一个带有 bindingsource 的 datagridview 包含三列,但其中一列包含 ComboBoxes。

使用下面的代码,我可以写入 xml 文件,但如何从中读取?感谢你的帮助

Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, _
                   ByVal e As EventArgs) Handles MyBase.Load
    Me.BindingSource2.DataSource = Me.GetParentTable()
    Me.Column1.DisplayMember = "ParentName"
    Me.Column1.ValueMember = "ParentID"
    Me.Column1.DataSource = Me.BindingSource2
    Me.BindingSource1.DataSource = Me.GetChildTable()
    With DataGridView1
        .DataSource = Me.BindingSource1
        .AllowUserToAddRows = True : .AllowUserToDeleteRows = True
        .AllowUserToOrderColumns = False : .AllowUserToResizeRows = True
    End With
End Sub
Private Function GetParentTable() As DataTable
    Dim table As New DataTable
    With table.Columns
        .Add("ParentID", GetType(Integer))
        .Add("ParentName", GetType(String))
    End With
    With table.Rows
        .Add(1, "Parent 1")
        .Add(2, "Parent 2")
        .Add(3, "Parent 3")
    End With
    Return table
End Function
Private Function GetChildTable() As DataTable
    Dim table As New DataTable ("IOBOARD")
    With table.Columns
        .Add("ChildID", GetType(Integer))
        .Add("ParentID", GetType(Integer))
        .Add("ChildName", GetType(String))
    End With
    With table.Rows
        .Add(1, 3, "Child 1")
        .Add(2, 2, "Child 2")
        .Add(3, 1, "Child 3")
    End With
    Return table
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Save To XML
    Dim dt As DataTable = CType(BindingSource1.DataSource, DataTable)
    dt.AcceptChanges()
    dt.WriteXml("c:\so\Test.xml", System.Data.XmlWriteMode.WriteSchema, False)
End Sub

结束类

4

1 回答 1

0

您是否尝试过dataset.ReadXml方法?

于 2013-06-07T20:39:00.743 回答