Listview - 使用数据库中的相应项填充第二列

时间:2014-04-09 14:39:56

标签: database vb.net listview

listview的fist列是从数据库填充的(数据库列名称KONTO - 仅以2020-开头的值),listview的第二列应该从相应的项目填充到同一数据库列NAZIV的KONTO到listview的第二列

enter image description here 描述位于数据库中的NAZIV列

我整天都在试验这个并没有取得任何成功。

这是我到目前为止的代码:

        ListView1.View = System.Windows.Forms.View.Details
    ListView1.Columns.Add("COL1", 100, HorizontalAlignment.Left) 'KONTO
    ListView1.Columns.Add("COL2", 160, HorizontalAlignment.Left) 'NAZIV



    Dim FilePath As String = "W:\GLAVNI\KOR14\"
    Dim DBF_File As String = "MATIKGL"
    Dim ColName As String = "KONTO"
    'Dim naz As String
    Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
    " ;Extended Properties=dBASE IV")
        con.Open()
        Using cmd As New OleDbCommand("SELECT * FROM MATIKGL ORDER BY KONTO, NAZIV", con)
            Using reader As OleDbDataReader = cmd.ExecuteReader()
                If reader.HasRows Then
                    While (reader.Read())
                        Me.ListView1.Items.Add(reader("KONTO"))

                        'LOOP BELOW SELECTS ALL ITEMS THAT STARTS WITH 2020-
                        For i = 0 To ListView1.Items.Count - 1
                            If ListView1.Items(i).ToString.Contains("2020-") Then

                            Else
                                ListView1.Items.Remove(ListView1.Items(i))
                            End If
                        Next


                    End While

                Else

                End If
            End Using
        End Using
        con.Close()
    End Using

感谢。

更新

更清楚,这是数据库中的屏幕。 enter image description here

只有以2020-(1)开头的项目应填充listview的第一列,第二列应填入" 2020 - "来自NAZIV(2)栏的描述。

1 个答案:

答案 0 :(得分:2)

每个LV项都有一个与之关联的SubItem集合。子项在“详细信息”视图中显示为列:

Dim lvi As ListViewItem              ' scratch var for adding items
Dim tmp As String

Using cmd As New OleDbCommand("SELECT * FROM MATIKGL ORDER BY KONTO, NAZIV", con)
        Using reader As OleDbDataReader = cmd.ExecuteReader()
            If reader.HasRows Then
                While (reader.Read())

                    ' Db rdr returns Object, cast to string
                    tmp = reader.Item("KONTO").ToString

                    ' no need to loop - just use the scratch vars
                    ' only adding if it contains 2020
                    If tmp.Contains("2020-") Then
                        lvi = New ListViewItem
                        lvi.Text = tmp

                        ' add the sub item
                        lvi.SubItems.Add(reader("NAZIV").toString)
                        ListView1.Items.Add(lvi)
                    End If
               End While
            End If
        End Using

你可以使用reader.Item("KONTO").ToString.Contains("2020")摆脱tmp var ...以上是为了清晰