0

大家好,我正在做一个小项目,我在文件而不是数据库中添加、删除和更新项目。我有一个列表视图和列表,我在 UI 中的列表视图中添加项目并在列表中添加对象。但是当我从列表视图中删除项目时,而不是列表中的对象并抛出异常。代码如下和行问题和问题的指示。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;


   namespace Address_Book_students
  {
    public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }
    List<student> boy = new List<student>();

    private void Form1_Load(object sender, EventArgs e)
    {
        String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        if (!Directory.Exists(path + "\\Address_project_irfan"))
            Directory.CreateDirectory((path + "\\Address_project_irfan"));
        if (!File.Exists(path + "\\Address_project_irfan\\setting.xml"))
            File.Create((path + "\\Address_project_irfan\\setting.xml"));

    }

    private void button2_Click(object sender, EventArgs e)
    {
        student b = new student();
        b.Name = textBox1.Text;
        b.Address = textBox2.Text;
        b.Email = textBox3.Text;
        b.Birthday = dateTimePicker1.Value;
        b.Additional_info = textBox4.Text;
        listView1.Items.Add(textBox1.Text);
        boy.Add(b); // when i add the items in the listview1 at same time object is 
                    // object is b is added to the lis boy
        textBox1.Text = "";
        textBox2.Text = "";
        textBox3.Text = "";
        dateTimePicker1.Value = DateTime.Now;
        textBox4.Text = "";


    }
    public void Remove()
    {
       listView1.Items.Remove(listView1.SelectedItems[0]);
        boy.RemoveAt(listView1.SelectedItems[0].Index);//But when i remove items from
                                                       //the listview1 the items deleted
                                                       //from the listview but the object
                                                       // is not deleting from the list 
                                                       //boy throwing the exception(Value of 0 is not
                                                       //a valid arguement) what the problems here with list?


    }

    private void button3_Click(object sender, EventArgs e)
    {
        Remove();
    }



}
public class student
{
    public string Name
    {
        get;
        set;

    }
    public string Address
    {
        get;
        set;

    }
    public string Email
    {
        get;
        set;

    }
    public DateTime Birthday
    {
        get;
        set;

    }
    public string Additional_info
    {
        get;
        set;

    }

}
 }
4

2 回答 2

3

listView1.SelectedItems[0]用于查看选择了哪个项目 - 两次,一次用于将其从 ListView 中删除,另一次用于将其从列表中删除。

不幸的是,在您从 ListView 中删除它后,它listView1.SelectedItems[0]不再存在,因此您会遇到异常。

解决此问题的最简单方法 - 先从列表中删除项目,然后从 ListView 中删除。

于 2013-06-08T21:49:03.743 回答
0

我认为您最好先获取该对象,然后从列表中删除该对象,然后重新绑定列表视图。

student stu = (student)boy.Where(s => s.Id == (int)listView1.SelectedValue)).Single();
boy.Remove(stu);

然后重新绑定您的列表视图。这确实表明你有一个男孩的 ID,所以它是独一无二的。但是您可以使用其他字段之一,但您希望它是唯一的。

于 2013-06-08T22:03:41.607 回答