0
private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            int i=listView1.SelectedItems.Count;
            int j = listView1.SelectedIndices.Count;
            for (int k = 0; i < listView1.SelectedItems.Count; k++)
            {
                listView1.Items[k].Remove();
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

When I am selecting values from listview why is it returning count = 0 ?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Jatin Gandhi
  • 23
  • 2
  • 8

4 Answers4

0

The property SelectedItems returns the items that are selected, i.e. marked by clicking on the text (usually with a blue highlight).

To get the items whose checkboxes are checked, use the CheckedItems property.

waldrumpus
  • 2,540
  • 18
  • 44
0

You are modifying the collection that you use in for statement. Take a look for removing item from collection.

Community
  • 1
  • 1
hkutluay
  • 6,794
  • 2
  • 33
  • 53
0

You can adjust your DataBind of ListView in : ! IsPostBack section

You persist your datas with ViewState

If(! IsPostBack)
{
  //You don't pass by this section when you post => you don'y erase your selected values 

  //Bind your ListView
}

Your datas are erased because when you post by clicking, you re-build your ListView et so erase selected values

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

You can use listview's property CheckedItems to check for all items that were checked.

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
roybalderama
  • 1,650
  • 21
  • 38