Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How to Add, Remove and Clear Items Form ListView Control

In this sample program I will try to explain you how you can add, remove and clear items form a ListView control using Visual Basic.Net (VB.Net).

ListView control allows you to display item text in a list. For example you are selecting files from a TreeView control using ListView control you can display all your selected files in a list. Using ListViewItem class you can access list of items present in a List. Each item that is added in a list forms a collection named as ListViewItemCollection. Here I will use three of its methods Items.Add, Item.Remove and Items.Clear.

 

To begin with this application adds four controls on your windows form (dialog box) and that are; Label Control, TextBox Control, ListView Control and finally three Button Controls named as cmdAddItem_Click, cmdRemoveItem_Click and cmdClear_Click. Code for these three events are as follows:

 

Private Sub cmdAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddItem.Click

 

      lvItemList.Items.Add(txtAddItem.Text & "
"
)

 

End Sub

 

Private Sub cmdRemoveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemoveItem.Click

 

 For Each lvItem As ListViewItem In lvItemList.SelectedItems

      lvItem.Remove()

 Next

 

End Sub

 

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click

 

      lvItemList.Items.Clear()

 

End Sub

Article Comments
The sample code is okay but what really disturbs programmers is the populating of a ListView control from a database. I wish you could give that example, seperately for SQLServer 2000 database and MS Access 2000 database

Posted on 9/7/2006 4:36:56 AM by Joses

Send me the code to add multiple item in listview using recored set in vb.Net 2005

Posted on 12/28/2006 4:50:35 AM by Subhash chougule

Send me the simple Code to add multiple column & Multiple row in listview in Vb.net 2005

Posted on 12/28/2006 4:54:03 AM by subhash chougule

Please I need your help on how to add items to a listview, Trewview from an SQL Server Database using recorset in vb.Net 2005
also on Datagrid

Thanks

Posted on 4/3/2007 7:02:21 AM by Victor Ezeliorah

The sample code is okay but what really disturbs programmers is the populating of a ListView control from a database.seperately for SQLServer 2000 database and MS Access 2000 database
i wish you give the example to my email, thanks

Posted on 7/10/2007 5:57:46 AM by sabar

Fantastic!!!!!!!
Brilliant code. I wrote several lines for the
same result. But It's far good than mine.
Please send code for populating records into the ListView from a any database.

Posted on 1/16/2008 6:03:09 AM by Pramod C. Pathak

You are a GENIUS!!! I have been trying to remove selected items from an imagelist (shown within a listview) for quite some time now, and this code is bloody brilliant!!! Thanks!

Posted on 11/28/2008 6:54:31 PM by David Sladdin

hi to all,, i need ur help how to save,update,search using vb.net application?

Posted on 12/15/2008 3:23:49 AM by joseph

your coding is better enough to appreciate and i like too...that's is why...and i have something to ask for you..can you give me a sample code of how to view specific records using listview or datagrid from the database?...

Posted on 12/2/2009 1:50:30 AM by roy

Hi roy,

The better option is that you filter your records in database query, it will speed up your process.
other wise you can use some events of datagrid like grid_OnItemDataBound event. Here you can filter your records


This event is fired immediately after ItemCreated event if item
supports data binding.
Here we are accessing the Datagrid Items (rows) and changing Tooltip of first columns inside that row.

public void grid_OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
WebControl webCtrl = (WebControl)e.Item.Cells[0];
webCtrl.ToolTip = "Tooltip value";
}
}


Thanks.

Posted on 12/2/2009 4:18:36 AM by zunnair

This code doesn't work... it doesn't compile because you can't do a new line in the middle of a string:

Private Sub cmdAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddItem.Click



lvItemList.Items.Add(txtAddItem.Text & "
")



End Sub

Posted on 12/18/2009 10:23:04 AM by Paul

Hi Paul,

You can write like this

lvItemList.Items.Add(txtAddItem.Text)

Posted on 12/21/2009 4:13:08 AM by zunnair

hi, how can i display list of names from ms access database (for example emp_names in employee table located in database) in a textbox when we are ready to enter another name in the same textbox and also on button click for modify and also delete

Posted on 2/2/2010 8:28:46 AM by purna

gud eveng
i am trying to developed a chain buissness software using vb.net 2005 n sql server 2005.but the problem i facing is in populating the listveiw from the database.
i will be thank full if u kindly help me ti sort out this problem as soon as possible.

Posted on 2/3/2010 1:59:47 AM by chandan

I LIKE REMOVE CLEAR PROGRAM SEND A FAX

Posted on 3/16/2010 6:36:46 PM by KAROL GLAZER

vb.net coding with ms access content

Posted on 4/3/2010 2:20:43 AM by manoj kumar

i want to add a list of data into listview using sql server 2005 database ,what the code is to doing this?

Posted on 5/10/2010 3:05:15 PM by marta tesfaye

Create database about Clinic by VB.net 2005 with access

Posted on 7/1/2010 11:00:00 PM by sadat

please how do i populate record from a listview into a textbox from different form

Posted on 8/2/2010 9:26:54 AM by kola

can this code download the item from database also....if no
then send me the code please...

Posted on 11/16/2010 9:09:02 AM by bhanu

can this code delete the item from database also....if no
then send me the code please...

Posted on 11/16/2010 9:09:33 AM by bhanu

hi....
i am making project of bus booking using the vb.net and sql server how to add,edit and delete the record using easily way.......plase help me...

Posted on 11/22/2010 1:52:58 AM by dhaval

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Create an Event Log, Event Sources and Entries

Disclaimer - Privacy
© 2002-2014 DevASP.net