MorkaLork Development

Interesting stuff I've picked up over the years...

Removing items

2009-04-17 07:20:26 | 566 views | csharp class method tutorial list view listview selectedindices checkboxes

The Basics



Removing an item in a listview is somewhat similar to removing an item from a list. You can choose to remove a single item by getting the index of an item:


int i = listView1.SelectedIndices[0];


The method SelectedIndices returns a SelectedIndexCollection which means that you have to select what index of the collection you want to get. If you have multiselect of the item you want will always be [0] since you'll only have single index collection.

If you however have a multiselect listview you might want to loop through it.

Getting into it



There are two ways of selecting items in a listview (by default); by checkbox or by row select.

Let's take a look at our listbox:
imagehttp://admin.morkalork.com/uploads/images/listviewRemove1.png

We have two listviews, one with checkboxes and one without. This article will show you how to remove from both listviews with the two separate methods.

First, if you want to remove from a checkboxed listview:


//Remove from listview with checkboxes
private void button1_Click(object sender, EventArgs e)
{
foreach (ListViewItem lvi in listView1.Items)
{
if (lvi.Checked == true)
listView1.Items.Remove(lvi);
}
}


You loop through every ListViewItem in the listview and you simply check the checked property for every item to see if it's checked. If it is, remove it.

The method for removing row selected items is quite similar:


//Remove from listview without checkboxes
private void button2_Click(object sender, EventArgs e)
{
foreach (ListViewItem lvi in listView2.SelectedItems)
{
listView2.Items.Remove(lvi);
}
}


We use the SelectedListViewItemCollection and pick out the selected items with the SelectedItems property. This also enables multiple selected rows deletion.

The full code




using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace listview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//==============SETUP================================================
//So that we can see the details :)
listView1.View = View.Details;
listView2.View = View.Details;
//Implement checkboxes
listView1.CheckBoxes = true;
listView2.CheckBoxes = false;
//So that the entire item (and subitem) is selected by the user
listView1.FullRowSelect = false;
listView2.FullRowSelect = true;
//Hide gridlines
listView1.GridLines = false;
listView2.GridLines = true;

//Since we don't want to have a listview with a scrollbar, we set
//the width at startup and apply this to the columns
listView1.Width = 260;
listView2.Width = 260;

//100 + 100 + 60 = 260, the total width of the listview
listView1.Columns.Add("Name", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Age", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Idiot", 60, HorizontalAlignment.Left);

listView2.Columns.Add("Name", 100, HorizontalAlignment.Left);
listView2.Columns.Add("Age", 100, HorizontalAlignment.Left);
listView2.Columns.Add("Idiot", 60, HorizontalAlignment.Left);

//===============LOADING=LIST1============================================
ListViewItem item1 = new ListViewItem("Rod");
item1.Checked = false;
item1.SubItems.Add("8");
item1.SubItems.Add("Yes");

ListViewItem item2 = new ListViewItem("Todd");
item2.Checked = false;
item2.SubItems.Add("10");
item2.SubItems.Add("Yes");

ListViewItem item3 = new ListViewItem("Lisa");
item3.Checked = true;
item3.SubItems.Add("10");
item3.SubItems.Add("No");

listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });

//===============LOADING=LIST2============================================
ListViewItem item4 = new ListViewItem("Burns");
item4.Checked = false;
item4.SubItems.Add("109");
item4.SubItems.Add("No");

ListViewItem item5 = new ListViewItem("Smither");
item5.Checked = false;
item5.SubItems.Add("48");
item5.SubItems.Add("No");

ListViewItem item6 = new ListViewItem("Wiggum");
item6.Checked = true;
item6.SubItems.Add("35");
item6.SubItems.Add("Yes");

listView2.Items.AddRange(new ListViewItem[] { item4, item5, item6 });
}

//Remove from listview with checkboxes
private void button1_Click(object sender, EventArgs e)
{
foreach (ListViewItem lvi in listView1.Items)
{
if (lvi.Checked == true)
listView1.Items.Remove(lvi);
}
}

//Remove from listview without checkboxes
private void button2_Click(object sender, EventArgs e)
{
foreach (ListViewItem lvi in listView2.SelectedItems)
{
listView2.Items.Remove(lvi);
}
}

}
}




Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.