ListView Control in VB.Net

The ListView control in VB.Net provides a versatile and visually appealing way to display a collection of items. The ListView control offers five different views to present the items: LargeIcon, Details, SmallIcon, List, and Tile. Each view has its own distinct layout and style, allowing developers to choose the most suitable presentation for their data.

What You Can Do with the ListView Control

The ListView control offers a versatile way to display and manage lists of items. You can populate it with text, icons, or a combination of both. It allows users to select single or multiple items, making it suitable for tasks like choosing files, browsing options, or displaying search results. You can even add checkboxes to your ListView for creating selectable lists with on/off options.

Add Columns in VB.Net ListView

To add columns to a ListView, the Columns.Add() method is utilized. This method enables you to define the columns and their respective properties within the ListView control. The Columns.Add() method accepts two arguments: the column heading and the column width.

listView1.Columns.Add("ProductName", 100)

In the above code, "ProductName" is column heading and 100 is column width.

By specifying the column heading as the first argument, you provide a descriptive label for the column, indicating the type of information or attribute being displayed in that column. This helps users understand the purpose or content of the column.

The second argument of the Columns.Add() method is the column width. This determines the width of the column within the ListView control. You can specify the width in pixels or as a relative value, depending on your specific requirements and layout preferences.

Add Item in VB.Net Listview

You can add items in listbox using ListViewItem which represents an item in a ListView control.

Dim arr As String() = New String(3) {} Dim itm As ListViewItem 'add items to ListView arr(0) = "product_1" arr(1) = "100" arr(2) = "10" itm = New ListViewItem(arr) listView1.Items.Add(itm)

Get selected item from VB.Net ListView

productName = listView1.SelectedItems(0).SubItems(0).Text

Above code will return the itme from first column of first row.

Sorting VB.Net Listview Items

If the Sorted property of Listview is set to true, then the ListView items are sorted. The following code sorts the ListView items:

ListView1.Sorted = True

Add Checkbox in Listview

You can add checkbox in VB.Net Listview columns.

myListView.CheckBoxes = True myListView.Columns.Add(text, width, alignment)
VB.Net Listview example

ListView provides a large number of properties that provide flexibility in appearance and behavior. The View property allows you to change the way in which items are displayed. and the SelectionMode property determines how many items in the list can be selected at a time.

The following Vb.Net program first set its view property as Details and GridLines property as true and FullRowSelect as true.

ListView1.View = View.Details ListView1.GridLines = True ListView1.FullRowSelect = True

Finally in the button click event, it will display the selected row values in a message box.

Full Source VB.NET
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ''Set view property ListView1.View = View.Details ListView1.GridLines = True ListView1.FullRowSelect = True 'Add column header ListView1.Columns.Add("ProductName", 100) ListView1.Columns.Add("Price", 70) ListView1.Columns.Add("Quantity", 70) 'Add items in the listview Dim arr(3) As String Dim itm As ListViewItem 'Add first item arr(0) = "product_1" arr(1) = "100" arr(2) = "10" itm = New ListViewItem(arr) ListView1.Items.Add(itm) 'Add second item arr(0) = "product_2" arr(1) = "200" arr(2) = "20" itm = New ListViewItem(arr) ListView1.Items.Add(itm) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim productName As String Dim price As String Dim quantity As String productName = ListView1.SelectedItems.Item(0).SubItems(0).Text price = ListView1.SelectedItems.Item(0).SubItems(1).Text quantity = ListView1.SelectedItems.Item(0).SubItems(2).Text MsgBox(productName & " , " & price & " , " & quantity) End Sub End Class


By utilizing the Columns.Add() method, you can enhance the ListView control by incorporating columns that organize and present your data in a structured and easily readable manner, improving the overall user experience and data visualization within your VB.Net application.