I have tried to remove specific items from a listview using the RemoveAt()
method. But When I remove it the first time some items will stay.
For example: see the image below
Code:
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].SubItems[0].Text == "A1")
{
listView1.Items.RemoveAt(i);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
for(int i = 0; i<3; i++)
{
ListViewItem lvi = new ListViewItem("A1");
lvi.SubItems.AddRange(new string[] {"desc" + i.ToString(), i.ToString()});
listView1.Items.Add(lvi);
}
for (int i = 0; i < 2; i++)
{
ListViewItem lvi = new ListViewItem("A2");
lvi.SubItems.AddRange(new string[] { "desc" + i.ToString(), i.ToString() });
listView1.Items.Add(lvi);
}
}
for
loop to start at the end and go to0
, you can get rid of thei--;
. – Alisun