VB.Net - ListView Control
List views displays a collection of items that can be displayed using one of five different views, such as LargeIcon, Details , SmallIcon, List and Tile.
Add Columns in VB.Net ListView
You can add columns in Listview by using Columns.Add() method. This method takes two arguments, first one is the Column heading and second one the column width.
listView1.Columns.Add("ProductName", 100) |
In the above code, "ProductName" is column heading and 100 is column width.
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) |
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.
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