VB.Net列表视图将数据库中的数据添加到多个列中

时间:2014-12-05 06:02:56

标签: database vb.net listview multiple-columns

我正在尝试将数据库信息放入listview。该数据库包括:

ID
LastName
FirstName
Extention
Department

有15条记录,每条记录基于以上列

采用以下格式
100  Simpson   Homer   2342  Animation

我想用ID, LastName, FirstName, Extention, Department以相同的格式显示每条记录。我可以用1列添加行,无论我在哪里,我都无法找到如何使用多列来显示所有数据

我的数据库是内部vb.net。我有15条记录,目前这种情况发生在form.load

'Form onLoad Event (Executes once the form has loaded)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Me.EmployeesTableAdapter.Fill(Me.EmployeeDataSet.Employees)

    Dim row As EmployeeDataSet.EmployeesRow

    'Setting the view type for list view (can also be done in the form properties)
    lstvEmployees.View = View.Details

    'Creating Columns in the List View
    lstvEmployees.Columns.Add("ID", 120, HorizontalAlignment.Left)
    lstvEmployees.Columns.Add("Last Name", 120, HorizontalAlignment.Left)
    lstvEmployees.Columns.Add("First Name", 120, HorizontalAlignment.Left)
    lstvEmployees.Columns.Add("Extension", 120, HorizontalAlignment.Left)
    lstvEmployees.Columns.Add("Department", 120, HorizontalAlignment.Left)

    ' Itterates Through each row in the Employees Database
    For Each row In EmployeeDataSet.Employees.Rows

        lstvEmployees.Items.Add(row.ID.ToString)

    Next

End Sub

感谢您的帮助,我希望我留下足够的信息,希望文档可以帮助那些不熟悉列表视图和数据库的人。

1 个答案:

答案 0 :(得分:1)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Me.EmployeesTableAdapter.Fill(Me.EmployeeDataSet.Employees)
    lvEmployees.FullRowSelect = True

    lvEmployees.Clear()
    Dim row As ListViewItem

    lvEmployees.Columns.Add("ID", 70, HorizontalAlignment.Left)
    lvEmployees.Columns.Add("Last Name", 120, HorizontalAlignment.Left)
    lvEmployees.Columns.Add("First Name", 120, HorizontalAlignment.Left)
    lvEmployees.Columns.Add("Extension", 120, HorizontalAlignment.Left)
    lvEmployees.Columns.Add("Department", 120, HorizontalAlignment.Left)

    ' Itterates Through each row in the Employees Database
    For Each employee As EmployeeDataSet.EmployeesRow In EmployeeDataSet.Employees

        row = New ListViewItem(employee.ID)


        row.SubItems.Add(employee.LastName)
        row.SubItems.Add(employee.FirstName)
        row.SubItems.Add(employee.Extension)
        row.SubItems.Add(employee.Department)

        lvEmployees.Items.Add(row)
    Next



End Sub

您必须创建一个列表视图,并且可以将其作为起始列。然后为每列的ListView添加子项。