vb.net ListView How it works?

Aug 20, 2013   //   by kdecom   //   vb.net  //  No Comments

Here i will explain how the list view works in VB.Net

Let just create an situation where you want to create an list view.

Example:- Let just say you are building an Point Of Sale VB.NET application.

As soon as you scan an item you need to fetch an item from database and append it into an listview.

How to create a Listview

Just simply drag the Listview from toolbar. Just Make sure you set the View property to Details

NOTE:- ** In most case this the problem that people forget it.



Change the name property of listview to ItemsListView


Next we need to add 4 columns into our Listview.

How to add Columns into ListView

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

        ItemsListView.Columns.Add("ID", 40)
        ItemsListView.Columns.Add("Name", 250)
        ItemsListView.Columns.Add("Qty", 70)
        ItemsListView.Columns.Add("Price", 100)


    End Sub

First argument is the “LABEL” Second argument is the width of an Column.

If you want to changed the size of an listview columns just changed the value above

So if you execute the command it would looks like this



Next When someone scan the item we need to fetch and append the value of an item into a list view

How to add Subitems into an listview


 Item = ItemsListView.Items.Add("ID_CHANGE_YOUR_ITEM_ID")
  With Item
 End With


One thing that you might need to do is Just change the qty of an item so in other words you want to changed the Listview Item text

How to changed Existing ListView Item Text

For Each myItem As ListViewItem In ItemsListView.Items()
     If myItem.Text = reader(0) Then
         'above line just an condition to check if it's an existing item... You can changed it based on your requirement.    
         ItemsListView.Items(myItem.Index).SubItems(0).Text = "MY_NEW_ID_VALUE"
         ItemsListView.Items(myItem.Index).SubItems(1).Text = "MY_NEW_ITEM_NAME_VALUE"
         ItemsListView.Items(myItem.Index).SubItems(2).Text = "MY_NEW_QTY_VALUE"
         ItemsListView.Items(myItem.Index).SubItems(3).Text = "MY_NEW_PRICE_VALUE"
    End If 




Next Step When someone finished paying to you in that case for your next customer you need to clear all the sub items into a list view

How to Clear Listview Subitems


If anyone got any problem with ListView please comment it and i hope all of us try to solve it asap.


Leave a comment