【问题标题】:VB.NET - DataSet, DataGrid and XML - Selecting tables and rowsVB.NET - DataSet、DataGrid 和 XML - 选择表和行
【发布时间】:2013-02-02 12:54:47
【问题描述】:

到目前为止,我一直在使用文本文件在我的应用程序中存储数据。它们是用逗号分隔的。我使用 ListView 和相当多的代码来呈现数据。

今天我了解了 DataSet 和 DataGrid 组件。我很想使用它们。然后我的数据可以存储在 XML 中,这很好,因为服务器也这样做。

不知道如何让它工作。我有一个简单的层次结构:

<myapp>
  <user>
    <firstname>John</firstname>
    <lastname>Doe</lastname>
  </user>
  <collection>
    <name>Beer cans</name>
    <item>
      <id>1</id>
      <name>Heineken</name>
    </item>
  </collection>
  <collection>
    <name>Coffee mugs</name>
    <item>
      <id>18</id>
      <name>Starbucks</name>
    </item>
  </collection>
</myapp>

通常我会将集合元素包装在父元素中,但这似乎使 VB.NET 中的事情变得复杂。无论如何。我的问题是……

如果我的应用中有一个用户可以点击的集合列表,我希望在 DataGrid 中显示该集合中的所有项目。

到目前为止,我的代码在某种程度上选择了正确的集合,但我似乎无法找到这些项目本身:

Dim DataSet As New DataSet
DataSet.ReadXml("c:\john.doe.xml")
MainGrid.DataSource = DataSet.Tables("collection").Select("name='" & selName & "'")

(编辑:错字)

【问题讨论】:

    标签: xml vb.net datagrid dataset


    【解决方案1】:
        If NetworkInterface.GetIsNetworkAvailable Then
            Dim cl As New WebClient
            AddHandler cl.DownloadStringCompleted, AddressOf cl_DownloadStringCompleted
            Dim url As String = "Your link in here"
            cl.DownloadStringAsync(New Uri(url))
        Else
            MessageBox.Show("check your internet connection first")
        End If
    
    Private Sub cl_DownloadStringCompleted(sender As Object, e As System.Net.DownloadStringCompletedEventArgs)
    Dim doc = XDocument.Parse(e.Result)
    For Each result In doc.<myapp>.<user>
            TextBlock1.Text = TextBlock1.Text & Environment.NewLine & result.<firstname>.Value
        Next
    End Sub
    

    我希望它有效:)

    【讨论】:

      【解决方案2】:

      试试这个:

      Dim CollectionTable As DataTable = DataSet.Tables("collection")
      Dim CollectionSelection As DataRow() = CollectionTable.Select("name='" & selName & "'")
      Dim CollectionID As Integer
      
      If CollectionSelection.Count = 1 Then
          CollectionID = CollectionSelection(0)("collection_id")
      Else
          Exit Sub
      End If
      
      MainGrid.DataSource = DataSet.Tables("item").Select("collection_id =" & CollectionID).CopyToDataTable()
      

      您的 DataSet 正在读取您的 xml 并创建 3 个表 usercollectionitemitem 表有 3 列 id name collection_id。所以我所做的是根据集合名称选择 collection_id 并根据该 ID 选择项目。请注意,集合表有 namecollection_id 列。

      【讨论】:

        猜你喜欢
        • 2012-12-27
        • 1970-01-01
        • 2012-06-27
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        • 2014-10-22
        • 1970-01-01
        • 2012-01-02
        相关资源
        最近更新 更多