Free Information Technology Magazines and eBooks

Wednesday, June 17, 2009

VB.NET ListView Tutorial. Add, Edit and Delete

One of my most commonly used VB.NET control to present a spread-sheet like information is the ListView control. According to msdn, a list-view control is a window that displays a collection of items using one of four different views. List-view controls provide several ways to arrange and display items and are much more flexible than simple List Boxes. On this article, I will discuss the basics of adding, editing and deleting items in listview. A complete sample project can be downloaded at the end of this post.


At the end of the tutorial, you will be able to do the following tasks:
1. Properly configure ListView to display spread-sheet information
2. Add Items on ListView
3. Edit Item on ListView
4. Delete Item on ListView

Here are the step-by-step to create our list view tutorial project.
1. Create a new project in VB.NET
2. Put the following controls as shown below on your initial form

VB.NET ListView Tutorial. Add, Edit and Delete

3. Set the ListView control and set the following properties from the Property window.
a. FullRowSelect = True
b. GridLines = True
c. HideSelection = False
d. MultiSelect = False
e. View = Details

4. Declare blnAdd variable that will indicate if Add button was pressed.

Dim blnAdd As Boolean 'indicator if add button was pressed


5. On your add button, put the following codes:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

If btnAdd.Text.ToLower() = "add" Then 'if caption is ADD then perform Add process
txtFirstName.Enabled = True
txtLastName.Enabled = True
btnAdd.Text = "Save"
btnEdit.Text = "Cancel"
btnDelete.Enabled = False
txtFirstName.Text = ""
txtLastName.Text = ""
blnAdd = True

Else 'save process
txtFirstName.Enabled = False
txtLastName.Enabled = False
btnAdd.Text = "Add"
btnEdit.Text = "Edit"
btnDelete.Enabled = True
If blnAdd Then
AddItemToListView()
Else
EditItemInListView()
End If

End If
End Sub


Also include these codes below the btnAdd_Click, these functions are for adding and editing listview items.


'''
''' Add listview items
'''

'''
Private Sub AddItemToListView()
'Usually the first unique colum is the root item
Dim lv As ListViewItem = ListView1.Items.Add(txtFirstName.Text)
'The remaining columns are subitems
lv.SubItems.Add(txtLastName.Text)
End Sub
'''
''' Edit Item in ListView
'''

'''
Private Sub EditItemInListView()
If ListView1.SelectedItems.Count > 0 Then 'make sure there is a selected item to modify
ListView1.SelectedItems(0).Text = txtFirstName.Text
ListView1.SelectedItems(0).SubItems(1).Text = txtLastName.Text
End If
End Sub


6. On the edit button, place the following codes.

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
If txtFirstName.Text.Length > 0 Then
If btnEdit.Text.ToLower() = "edit" Then 'if caption is EDIT then perform EDIT process
txtFirstName.Enabled = True
txtLastName.Enabled = True
btnAdd.Text = "Save"
btnEdit.Text = "Cancel"
btnDelete.Enabled = False
blnAdd = False
Else 'cancel process
txtFirstName.Enabled = False
txtLastName.Enabled = False
btnAdd.Text = "Add"
btnEdit.Text = "Edit"
btnDelete.Enabled = True
End If
Else
MessageBox.Show("Please select record to edit")
End If

End Sub


7. While on the delete button, copy-and-paste these codes:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If ListView1.SelectedItems.Count > 0 AndAlso MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then 'make sure there is a selected item to delete
ListView1.SelectedItems(0).Remove()
End If
End Sub


8. To select an item and put the values on the respective textboxes, use the following codes:

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 Then
txtFirstName.Text = ListView1.SelectedItems(0).Text
txtLastName.Text = ListView1.SelectedItems(0).SubItems(1).Text
End If
End Sub


To see and examine the complete project, you can download it here.

To stay up-to-date on Technology news, subscribe now.

30 comments:

Anonymous said...

hi..i'm a beginner...thats a really nice tutorial...can you make a program like this but has a database?...

Anonymous said...

thanks, this helped me out

Anonymous said...

thanks this was very helpful.

Anonymous said...

hi. ur code really helps. .

can you give codes that connects to the database?

kram said...

what if i create another form for the fields that i wanted to be filled up and then the listview is on the another form what should i do?

Fryan Valdez said...

@kram
- You can create public variables on the popup form. fill-up these variables when you hit the save or ok button to return back on the form w/ listview.

Private Sub AddItem()
Dim frmNew as New frmNewItem
frmNew.ShowDialog()
Dim lv As ListViewItem = ListView1.Items.Add(frmNew.FirstName)
lv.SubItems.Add(frmNew.LastName
)
End Sub

Anonymous said...

@ fryan

i'm still confused coz this is what i wanted to happen oK??
i create a system in vb.net its a simple student registration ok..then i have 2 forms 1st contains a listview and the ADD,EDIT,DELETE buttons...the 2nd form contains the fields that i wanted to be fill up by the user..this is want i want to happen when i click btnADD appears the 2nd form then fill up fields and then added to the 1st form which contains the listview..and of course it has a database wherein data is stored..please help i need it on my project

Fryan Valdez said...

Please refer to my last answer. You have to create variables on the popup form and on the SAVE button of your popup form.. pass all the information on text boxes into the variables.


'POPUP FORM
Public FirstName As String
Public LastName As String

Private Sub btnSave_Click()
FirstName = txtFirstName.Text
LastName = txtLastName.Text
Me.Close
End Sub

'PARENT FORM WHERE LISTVIEW IS LOCATED

Private Sub btnAdd_Click()
Dim frmNew as New frmNewItem
frmNew.ShowDialog()
Dim lv As ListViewItem = ListView1.Items.Add(frmNew.FirstName)
lv.SubItems.Add(frmNew.LastName
)
End Sub

Hope this help

Anonymous said...

Good tutorial, thanks much.
I made it even better by adding LINQ queries, and a load event to connect it to data. Its a really simple alternative to using a DatagridView.

Anonymous said...

Simple but very useful

Anonymous said...

Simple but very useful

Anonymous said...

hehehe thnks sa code heheh very useful

Anonymous said...

Everything worked great but none of the names I add to the Listview stays there after closing the application. There doesn't seem to be any code to create a XML file to store the saved names on the listview. Help!

Monika said...

thanx a lot,superb coding,simple and easy to use.

Anonymous said...

can u help me with regards on SQL databse.. how can i store a listview items into the databse.. thanks alot.

Anonymous said...

how about search i need a tutorial for search and display in the listview

Anonymous said...

how about a search i want a tutorial for search and display the result in the listview

mark said...

@Fryan Valdez

I need help;

If my 1st listview is in form1.vb then my 2nd listview is in form2.vb what will be the code or syntax if my items in 1st listview in form1.vb is the same with the items in 2nd listview in form2.vb... and must update the data's....

Anonymous said...

Hi, could you please help me out in C# if you know one.

I'm doing a program in C#, wherein after populating a treeview from an XML file, I should double click a node that corresponds as a subnode or childnode, and display in a search result box. after double clicking the node, it application(processes) must run as where that xml file corresponds..thank you a lot..

Anonymous said...

hello,

thats a very nice tutorial.
you can help me??

we cant the programm save in c:\test.txt

thx

Anonymous said...

sorry, i forget my email.
the adress is 18196@gmx.de

thx for your help

Sy Atun said...

Hello Sir Fryan, if you dont mind can you help me with my thesis. My group and I are using visuual studio 2010 and some of your codes helped me a lot.. please. ang hirapo kasi! buti na lang your filipino. ang galing nyo po.. :) emeail: sb.atun23@gmail.com

Hopre you can help us. :( tnx!

Anonymous said...

How About Search??

Anonymous said...

can you give me codes to connect it to database using MS Access database???

CristiC said...

Very good post !!!
Thanks !!

Anonymous said...

I NEED YOUR HELP
What if I want to have many columns? I made a Contacts project. It has many SubItems like Address, Email, Mobile no., fb, twitter. How do I modify your codes? It doesn't work :(( please help me.

Aston said...

Please refer to my last answer. You have to create variables on the popup form and on the SAVE button of your popup form.. pass all the information on text boxes into the variables.


'POPUP FORM
Public FirstName As String
Public LastName As String

Private Sub btnSave_Click()
FirstName = txtFirstName.Text
LastName = txtLastName.Text
Me.Close
End Sub

'PARENT FORM WHERE LISTVIEW IS LOCATED

Private Sub btnAdd_Click()
Dim frmNew as New frmNewItem
frmNew.ShowDialog()
Dim lv As ListViewItem = ListView1.Items.Add(frmNew.FirstName)
lv.SubItems.Add(frmNew.LastName
)
End Sub

Hope this help



Dear Fryan Valdez

Can you make exaple please? i tri to make , but is not working(
I try to paste data from form 2 to form 1 (where is located listview)

Thx

Anonymous said...

hi, how if i want to display the data but there empty data for one of the data such as i don't have last name. i wish to just display first name and the last name is empty? I've no idea how to do so. It turn up error if the data is empty in database.

Anonymous said...

Hi, how can i display a data where there is empty data for example i wish to display only first name and in my database lastname is empty, i just want to display firstname and lastname is empty. how can i do so? i've no idea how is it. Thx in advance.

Kenny Redosendo said...

panu po to i connect sa ms access database?