Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLoad a ListView control from a database in Visual Basic .NET
DescriptionThis example shows how to load a ListView control from a database in Visual Basic .NET.
KeywordsListView, load ListView, database, ADO.NET, Access, VB.NET
CategoriesDatabase, VB.NET, Controls
 
When the program loads, the following code executes. It clears the ListView control and creates columns on it.

Next the program calls subroutine GetDbConnection to open a connection to the database. The program makes an OleDbCommand object to select data from the Books table and executes the command. For each returned record, the code adds the Title field's value to the ListView as a new item. It uses the other retrieved fields to add sub-items to the new item.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ListView1.Items.Clear()

    ' Create the column headers.
    ListView1.Columns.Add("Title", 10, _
        HorizontalAlignment.Left)
    ListView1.Columns.Add("URL", 10, _
        HorizontalAlignment.Left)
    ListView1.Columns.Add("ISBN", 10, _
        HorizontalAlignment.Left)
    ListView1.Columns.Add("Pages", 10, _
        HorizontalAlignment.Right)
    ListView1.Columns.Add("CD", 10, _
        HorizontalAlignment.Left)
    ListView1.Columns.Add("Year", 10, _
        HorizontalAlignment.Right)

    ' Start with details view.
    mnuView_Click(mnuViewDetails, Nothing)

    ' Load the data.
    ' Open the database.
    Dim conn As OleDbConnection = GetDbConnection()

    ' Select records.
    Dim cmd As New OleDbCommand( _
        "SELECT * FROM Books ORDER BY Title", _
        conn)
    Dim data_reader As OleDbDataReader = cmd.ExecuteReader()
    Do While data_reader.Read()
        Dim new_item As New _
            ListViewItem(data_reader.Item("Title").ToString)
        new_item.SubItems.Add(data_reader.Item("URL").ToString)
        new_item.SubItems.Add(data_reader.Item("ISBN").ToString)
        new_item.SubItems.Add(data_reader.Item("Pages").ToString)
        new_item.SubItems.Add(data_reader.Item("CD").ToString)
        new_item.SubItems.Add(data_reader.Item("Year").ToString)
        ListView1.Items.Add(new_item)

        Debug.WriteLine(new_item.Text & " : " & _
            new_item.SubItems(0).Text & ", " & _
            new_item.SubItems(1).Text & ", " & _
            new_item.SubItems(2).Text & ", " & _
            new_item.SubItems(3).Text & ", " & _
            new_item.SubItems(4).Text)
    Loop

    ' Close the connection.
    conn.Close()

    ' Size the columns to fit the data.
    Dim wid As Integer
    For i As Integer = 0 To ListView1.Columns.Count - 1
        ListView1.Columns(i).Width = -2
        wid += ListView1.Columns(i).Width
    Next i
    Me.ClientSize = New Size(wid + 20, Me.ClientSize.Height)

    ' Associate the ImageLists with the
    ' ListView's Icons and SmallIcons properties.
    'ListView1.Icons = imgLarge
    'ListView1.SmallIcons = imgSmall
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated