更新列表<class> </class>

时间:2011-04-28 06:01:25

标签: c# .net list

我有一个类列表,如何在特定列表位置编辑该类。

我正在尝试更新List<Class>,但我不知道如何将值传递回类。 即我不知道如何调用位置2的列表将类值1,2,3,4,5更改为6,7,8,9,0

该类是一个表单,我想要使用的方法基本上是:

public FormStudent(int a, string b, int c, double d, char f)
{
    textBoxID.Text = a.ToString();
    textBoxName.Text = b;
    textBoxCredits.Text = c.ToString();
    textBoxTuition.Text = d.ToString();
    if (f == 'R')
        radioButtonResident.Checked = true;
    else
        adioButtonNonResident.Checked = true;
}

我的清单是:

  private List<Student> studentList = new List<Student>();

哦!并且使我从listView通过

获取列表值变得更加困难
private void buttonUpdate_Click(object sender, EventArgs e)
{
    Student stu = new Student();

    ListView.SelectedListViewItemCollection selectedItems = listView1.SelectedItems;
    int count = selectedItems.Count;
    for (int i = 0; i < count; i++)
    {                   
        // I NEED THE UPDATE HERE TO CALL \/
        FormStudent stuInfoForm = new FormStudent(stu.Id, stu.Name, stu.Credits, stu.Tuition, stu.Residency);

        studentList.RemoveAt(i);
        stuInfoForm.Owner = this;
        stuInfoForm.ShowDialog();
    }
    refreshList();
}

3 个答案:

答案 0 :(得分:2)

只需将其编入索引:

List<int> list = new List<int>();
list.Add(1);
list.Add(2);
...
list.Add(5);

list[2] = 7;

答案 1 :(得分:0)

也许你错过了数据绑定?实际上很难理解你在做什么。看起来你在创建它们的同时正在删除学生,看起来很奇怪。

答案 2 :(得分:0)

我只使用学生的Id成员通过,但你可以通过它。

基本上你的主要形式是:

 private List<Student> studentList = new List<Student>();

 private void listView1_DoubleClick(object sender, EventArgs e) {
        ListView.SelectedListViewItemCollection selectedItems = listView1.SelectedItems;
        if (selectedItems != null && selectedItems.Count > 0) {
            ListViewItem item = selectedItems[0];
            Form2 form = new Form2(item.Text);
            form.Owner = this;
            form.ShowDialog();

            // Now get the values from the form.
            Student updateStudent = studentList.Find(o => o.Id == form.Student.Id);
            if (updateStudent != null) {
                updateStudent.Id = form.Student.Id;
                // Update the rest of the members.
            }

            // Re-populate your list using the updated student list.
        }
    }

现在以你的第二种形式学生形式:

    private Student _student = new Student();

    public Form2(string id) {
        InitializeComponent();
        textBox1.Text = id;
    }

    public Student Student {
        get {
            return _student;
        }
    }

    private void button1_Click(object sender, EventArgs e) {
        _student.Id = Convert.ToInt32(textBox1.Text);
        this.Close();
    }