VB.NET:将数据库列加载到listview中

时间:2013-12-30 15:56:59

标签: listview

我正在尝试将访问数据库表加载到listview的列中。我已设法加载数据库的前4列,但第5列存在问题。

Public Sub DisplayPasswords()
    Passes.Items.Clear()
    Dim dt As New DataTable
    Dim ds As New DataSet
    ds.Tables.Add(dt)

    Dim da As New OleDbDataAdapter("Select * from passwords", con)
    da.Fill(dt)

    Dim myRow As DataRow

    For Each myRow In dt.Rows
        Passes.Items.Add(myRow.Item(1))
        Passes.Items(Passes.Items.Count - 1).SubItems.Add(myRow.Item(2))
        Passes.Items(Passes.Items.Count - 1).SubItems.Add(myRow.Item(3))
        Passes.Items(Passes.Items.Count - 1).SubItems.Add(myRow.Item(4))
        Passes.Items(Passes.Items.Count - 1).SubItems.Add(myRow.Item(5))
    Next
End Sub

错误:

Overload resolution failed because no Public 'Add' can be called with these arguments:

'Public Function Add(item As System.Windows.Forms.ListViewItem.ListViewSubItem) As System.Windows.Forms.ListViewItem.ListViewSubItem':

这是我收到的错误的图片:http://i.imgur.com/lG627fc.png

这是我的访问数据库的图像:http://i.imgur.com/U0ZKrlR.png

这是我的列表视图的图片:http://i.imgur.com/RjpDVTy.png

2 个答案:

答案 0 :(得分:1)

我没有发现问题,但我找到了另一种方法来做同样的事情。

Public Sub DisplayPasswords()
    Main.Passes.Items.Clear()
    Dim cmd As New OleDb.OleDbCommand( _
     "SELECT * FROM passwords ORDER BY id", _
     con)
    Dim dr As OleDbDataReader = cmd.ExecuteReader()

    Do While dr.Read()
        Dim new_item As New  _
 ListViewItem(dr.Item("website").ToString)
        new_item.SubItems.Add(dr.Item("username").ToString)
        new_item.SubItems.Add(dr.Item("password").ToString)
        new_item.SubItems.Add(dr.Item("dates").ToString)
        new_item.SubItems.Add(dr.Item("notes").ToString)

        Main.Passes.Items.Add(new_item)
    Loop
End Sub

答案 1 :(得分:0)

当索引从0变为n-1,其中n是项目数时,请尝试:

For Each myRow In dt.Rows
    Passes.Items.Add(myRow.Item(0))
    Passes.Items(Passes.Items.Count - 1).SubItems.Add(myRow.Item(1))
    Passes.Items(Passes.Items.Count - 1).SubItems.Add(myRow.Item(2))
    Passes.Items(Passes.Items.Count - 1).SubItems.Add(myRow.Item(3))
    Passes.Items(Passes.Items.Count - 1).SubItems.Add(myRow.Item(4))
Next

但我不确定你的代码在做什么,你可能会遇到其他问题......