Home > DeveloperSection > Forums > Remove selected rows from multi-column listView in C#
Ashish Pandey

Total Post:128

Points:898
Posted on    July-17-2013 3:42 AM

 C# C# 
Ratings:


 1 Reply(s)
 5195  View(s)
Rate this:
Hi Developers! 

I have a listview with two columns and I'm using a context menu to allow users to remove selected rows. To remove the selected rows, I've tried with the following code but it doesn't work:

private void toolStripMenuItem1_Click(object sender, EventArgs e)

{

    listView1.SelectedItems.Clear();

}

I suspect this is because the listview has two columns, but I can't figure out a solution to remove selected rows. Removing all rows works with: listView1.Items.Clear();.

Thanks in Advance



shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    July-17-2013 8:24 AM

Solution!

The latest example of BeefTurkey looks correct, but he should decrement the variable i after removing a selected item:

for (int i = 0; i < listView1.Items.Count; i++ )

{

    if (listView1.Items[i].Selected)

    {

        listView1.Items[i].Remove();

        i--;

    }

}

The index of items larger as I is decremented by 1 after the removal. So you should reposition i to match the next not tested item.


Don't want to miss updates? Please click the below button!

Follow MindStick