Removing items
Written by: maffelu , 2009-04-17 07:20:26
int i = listView1.SelectedIndices[0];
//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);
}
}
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);
}
}
}
}
There are 2 comments on this article.
Maffelu
2010-10-05 22:09:54
Give the items you add an Id property that represents the databse Id of the item. When the item is removed you delete the item with the corresponding Id in the db.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.
coke
2010-10-05 15:32:34
how can i delete in the listview but too in database in c sharp and access