开发者

Linked viewlist/list removing selected items

I am a little unclear in the following so to clear things up my ultimate goal is to creat开发者_C百科e a button to remove a value displayed in a listview, the value is a list item.

This is the sort of thing I want...

studentList.Remove(listView1.SelectedIndices);
refreshList(); 

of course if that worked I would be in heaven, but it throws errors of course...

The list is private List<Student> studentList = new List<Student>() it is generated from another windows form.

I am having a problem removing items from a list/viewlist I am using.

Here is the code that keeps throwing the errors:

private void buttonDelete_Click(object sender, EventArgs e)
{
    // studentList.RemoveAt(listBoxStudent.SelectedIndex);

    for (int i = 0; i < listView1.Items.Count; i++)
    {
        studentList.RemoveAt(listView1.Items[i].Selected));
        i--;
    }
    refreshList();
}

The values are being put into the listview properly but I cannot seem to find a way to delete the values from the listview and the list together (refreshlist() clears and repopulates the listview according to the list studentList)

private void refreshList()
{
    listView1.Items.Clear();
    foreach (Student stu in studentList)
    {
        string[] filler = new string[5];
        filler[0] = stu.Name.ToString();
        filler[1] = stu.Residency.ToString();
        filler[2] = stu.Credits.ToString();
        filler[3] = stu.Tuition.ToString();

        listView1.Items.Add(stu.Id.ToString()).SubItems.AddRange(filler);

        listView1.View = View.Details;
    }
}

edit: added refreshlist() as requested.


Chris,

Dude, just remove the object from the list (forget RemoveAt(index)), then refresh the View... it will (or atleast it should) pickup the changes from "the model" (i.e. the students-list).

List.Remove Method: http://msdn.microsoft.com/en-us/library/cd666k3e.aspx

Cheers. Keith.


try changing your loop to

            for(int i = studentList.Items.Count -1; i>=0; i--)
            {
                if(studentList.Items[i].Selected == true)
                    studentList.Items[i].Remove();
            }

or in another way

            foreach (ListViewItem lvi in studentList.Items)
            {
                if (lvi.Selected == true)
                    lvi.Remove();
            }


You wouldn't generally try to manipulate both. Modify the underlying datasource (in your case, studentList), then update the ListView. If you modify your studentList to use an ObservableCollection, it will actually update the UI for you.

Also, if your collection's data type supports IComparable, you should just be able to use listView1.Remove(), rather than RemoveAt().


You are changing the collection that you are trying to iterate through. Once you have deleted the first item you have changed the list so the for loop will give you an error. The example given that iterates backwards (i--) is one way to solve the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜