user4062904
user4062904

Reputation:

VB.Net List View Add data from database into Multiple Columns

I am trying to put database information into a listview. The database consists of:

ID
LastName
FirstName
Extention
Department

There are 15 records and each record is in the following format based on the above columns

100  Simpson   Homer   2342  Animation

I would like to display each record in the same format with ID, LastName, FirstName, Extention, Department. I am able to add rows in with 1 column and no matter where I look I can't find how to do multiple columns to display all the data

My database is internally vb.net. I have 15 records, and this happens currently in 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

Thank you for your help, I hope I left enough information and I hope the documentation helps others who are not familiar with list views and databases.

Upvotes: 0

Views: 5421

Answers (1)

user4062904
user4062904

Reputation:

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

You must create a listview and you can give it its starting column. Then you add subitems to that ListView for each column.

Upvotes: 1

Related Questions