JG Vimalan's Blog

It is all about coding!

Deleting the selected items in listview at runtime using C#

Hi, 

Consider a scenario where, you have a listview control in your application. Each record in the listview control holds a xmlnode object in its Tag property.

User selects few items (rcords) in listview at random and you need to save the selected node into a list and then delete the selected records from the listview at runtime. 

To accomplish this scenario, the following code will help you…          

//Generic list to hold the selected nodes 

List<XmlNode> seletedItemsInList = new List<XmlNode>();      

//working from a high index keeps lower indexes at their original value
            for (int i = _ListView.Items.Count – 1; i >= 0; i–)
            {
                if (_ListView.Items[i].Selected)
                {
                    seletedItemsInList.Add(_ListView.Items[i].Tag as XmlNode);
                    _ListView.Items[i].Remove();
                }
            }
           
Note: if you work low to high, indexes change and you do not get the right results 

Regards,

JG Vimalan

Advertisements

August 10, 2009 - Posted by | C#.NET

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: