【问题标题】:How to Query a Dataset with multiple tables from XML file in VB.NET to populate a DataGrid如何在 VB.NET 中从 XML 文件中查询具有多个表的数据集以填充 DataGrid
【发布时间】:2014-12-18 08:05:39
【问题描述】:

这是我第一次在这里发帖,我绝对是 VB.NET 的新手,所以如果我做错了什么,请原谅我。

我正在尝试创建一个简单的 GUI 来更新我们用于登录脚本的驱动器映射的 XML。如果这是执行登录脚本的不好方法,请不要 cmets。

我有一个表单,上面有一个组合框和数据网格。我需要做的是用我已经完成的 OU 名称值填充组合框。现在我无法完成的是使用每个 OU 的映射填充 DataGrid。我想不通的是,当我将 XML 读入数据集时,它会创建两个表、OU 和映射。我想做的是,当我从 ComboBox 中选择一个 OU 时,让它仅使用来自该 OU 的映射填充 DataGrid。两个表中有一个共同的 OU_Id 字段。我认为这应该像对两个表的选择查询一样简单,但对于我的生活,我无法弄清楚如何完成。请帮帮我。

这是 XML 文件的格式。

<OUs>
    <OU name="ABC - NYO">
        <mapping path="" drive="F:"/>
        <mapping path="\\nyopr901\apps_prod" drive="G:"/>
        <mapping path="" drive="I:"/>
        <mapping path="" drive="J:"/>
        <mapping path="" drive="K:"/>
        <mapping path="" drive="L:"/>
        <mapping path="" drive="M:"/>
    </OU>
    <OU name="ABC - CAL">
        <mapping path="" drive="F:"/>
        <mapping path="\\nyopr901\apps_prod" drive="G:"/>
        <mapping path="" drive="I:"/>
        <mapping path="" drive="J:"/>
        <mapping path="" drive="K:"/>
        <mapping path="" drive="L:"/>
        <mapping path="" drive="M:"/>
    </OU>
</OUs>

这是我目前的代码。

Public Class frmLoginScriptMaintenance
Public dsDM As New DataSet()

Private Sub frmLoginScriptMaintenance_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Read in XML from file
    DriveMappings()
    For i = 0 To dsDM.Tables("OU").Rows.Count - 1
        ComboBox1.Items.Add(dsDM.Tables("OU").Rows(i).Item(1))
    Next
    ' Bind DataSet to Data Grid
    'grdData.DataMember = "mapping"
    'grdData.DataSource = dsPubs
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim dsDrives As New DataSet()
    Dim expression As String
    ' Read in XML from file
    ' Bind DataSet to Data Grid
**THIS IS WHERE I AM GOING WRONG**
    grdData.DataMember = "mapping"
    grdData.DataSource = dsDM.Tables(1).Select(**NOT SURE WHAT SHOULD GO HERE**)
End Sub

Public Function DriveMappings() As DataSet
    dsDM.ReadXml("DriveMappings.xml")
    Return dsDM
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

End Sub
End Class

【问题讨论】:

    标签: xml vb.net datagrid


    【解决方案1】:

    当您的 XML 加载到 DataSet 中时,会在两个表之间创建关系。您可以使用DataRow.GetChildRows 方法导航此关系。

    因此,例如,您可以使用以下方式从 OU 转到相关映射:

    Dim ouRow As DataRow = dsDM.Tables("OU").Rows(0)
    ' OU_mapping is the default name for the relationship that is generated
    Dim mappingRows() as DataRow = ouRow.GetChildRows("OU_mapping")
    

    您可以使用ComboBox1.SelectedIndexSelectedIndexChanged 方法中查找OU 表中的相应行。

    更新

    如您所见,绑定到DataRow 数组并不能真正满足您的需求。相反,如果您更改 ComboBoxDataGridView 绑定,则应自动跟踪 ComboBox 选择和 DataGridView 行之间的关系。

    Private Sub frmLoginScriptMaintenance_Load(...
        ' Read in XML from file
        DriveMappings()
        ' Bind the ComboBox to the OU table, showing the name column
        ComboBox1.DataSource = dsDM
        ComboBox1.DisplayMember = "OU.name"
        ' Bind the DataGridView to the mapping table, via the relationship, to show
        ' just the rows related to the selected OU
        grdData.DataSource = dsDM
        grdData.DataMember = "OU.OU_mapping"
    End Sub
    

    您根本不需要ComboBox1.SelectedIndexChanged 处理程序。

    注意:在 .NET 4.5.2 项目中使用标准 ComboBoxDataGridView 组件进行测试。

    【讨论】:

    • 我无法让它工作。我离得更近了。这是我改变的。 Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim ouRow As DataRow = dsDM.Tables("OU").Rows(0) ' OU_mapping 是生成的关系的默认名称 Dim mappingRows() As DataRow = ouRow.GetChildRows("OU_mapping") grdData.DataSource = mappingRows End Sub 现在数据网格正在放置正确数量的行,但它具有像 RowError、RowState、Table、HasErrors 这样的列名
    • 嗯,是的 - 绑定到 DataRow 的数组可能看起来不太好!哎呀。 :-) 我将更新答案以显示如何绑定事物并让它自动跟踪主/从关系。
    • 马克,非常感谢。我不知道为什么我不考虑 Combobox 是否能够使用数据源,这太棒了。我会为您的答案 +1,除非我显然还太新,不能这样做。非常感谢您的帮助。这个小项目让我兴奋地重新开始编码。我已经这么多年没有编程了。
    猜你喜欢
    • 2013-12-26
    • 2013-07-02
    • 1970-01-01
    • 2013-07-30
    • 2016-07-17
    • 2013-09-18
    • 2017-04-19
    • 2015-08-27
    • 2020-04-12
    相关资源
    最近更新 更多