分割列表视图项目需要帮助

时间:2014-01-16 12:38:09

标签: vb.net visual-studio-2010 visual-studio listview

我有一些代码循环遍历listview并将值放入'output1'& 'OUTPUT2'。然而,正在发生的是结果如此串联:0307NG77775660NG7778。如何拆分以便我可以包含在访问数据库中。

列表视图中有2列,它们包含以下格式的值:

第1行0307 - NG7777,第2行5660 - 7778等。这是桌面winforms代码。感谢

        Dim BoxList As New List(Of String)
        Dim BoxItem As String
        Dim CustRefList As New List(Of String)
        Dim CustRef As String

        For Each item As ListViewItem In Me.lvSelectedItems.Items
            Dim box As String = item.Text
            BoxItem = item.SubItems.Item(1).Text
            BoxList.Add(box)
            output2 += BoxItem

            Dim cust As String = item.Text
            CustRef = item.SubItems.Item(1).Text
            CustRefList.Add(cust)
            output1 += CustRef
        Next

        output = String.Join(" "c, BoxList.ToArray(), CustRefList.ToArray())

        Dim cmd As OleDbCommand = New OleDbCommand("Insert into Temp (temp, [class]) Values ('" & output1 & "', '" & output2 & "')", oledbCnn)
        dr = cmd.ExecuteReader

1 个答案:

答案 0 :(得分:1)

将用户名设为List(Of String),然后执行:

usernameList.Add(username) 'instead of output1 += username

完成循环后,请执行以下操作:

output1 = String.Join(" "c, usernameList.ToArray())

您可以选择自己选择的连接字符(如果您不喜欢空格" ")。

编辑:示例:

Dim usernameList As New List(Of String)
Dim session As String

For Each item As ListViewItem In Me.lvSelectedItems.Items
  Dim username As String = item.Text
  session = item.SubItems.Item(1).Text
  usernameList.Add(username)
  output2 += session
Next

output1 = String.Join(" "c, usernameList.ToArray())