我想更新数据,但它向我显示InvalidCastException

时间:2012-08-18 19:17:29

标签: c# c#-4.0 c#-3.0

我有Classess:人,学生,老师

人类是:

public class Person
{
    private string firstname;
    private string lastname;
    private string email;

    public Person()
    {
    }
    public int CompareTo(Person b)
    {

        return this.Email.CompareTo(b.Email);
    }

    public Person(string firstname, string lastname, string email)
    {
        this.Firstname = firstname;
        this.Lastname = lastname;
        this.Email = email;
    }

    public string Firstname{get;set;}       
    public string Lastname{get;set;}
    public string Email{get;set;}


    public virtual string  GetDisplayText(string sep)
    {
        return Firstname + sep + Lastname + sep + Email;
    }

}

学生班:

public class Students : Person
{
    private double assessmentGrade;
    private double assignmentGrade;

    public Students()
    {
    }

    public Students(string firstname, string lastname, 
                    double assessmentGrade,double assignmentGrade,
                    string email) : base(firstname, lastname, email)
    {
        this.AssessmentGrade = assessmentGrade;
        this.AssignmentGrade = assignmentGrade;
    }

    public double AssignmentGrade{get;set;}
    public double AssessmentGrade{get;set;}

    public override string GetDisplayText(string sep)
    {
        return base.GetDisplayText(sep) + "   " + 
               AssessmentGrade + "   " + AssignmentGrade;
    }

}

教师班:

public class Teacher : Person
{
    private int roomNumber;

    public Teacher()
    {
    }

    public Teacher(string firstname, string lastname, int roomNumber,
        string email) : base(firstname, lastname, email)
    {
        this.RoomNumber = roomNumber;
    }

    public int RoomNumber{get;set;}
     public override string GetDisplayText(string sep)
    {
        return base.GetDisplayText(sep) + " , RoomNo " + RoomNumber;
    }

}

我还有一个PersonList类:

public class PersonList : List<Person>
{
    List<Person> person = new List<Person>();

    public delegate void ChangeHandler(PersonList persons);
    public event ChangeHandler Changed;

    public bool Contains(string email)
    {
        return this[email] != null;
    }

    public new void Add(Person p)
    {
        base.Insert(0, p);
    }

    // Provide two additional methods
    public void Fill()
    {
        List<Person> persons = PersonDB.GetPersons();
        foreach (Person person in persons)
            base.Add(person);
    }

    public void remove(Person persons)
    {
        person.Remove(persons);
    }

    public static PersonList operator +(PersonList persons, Person p)
    {
        persons.Add(p);
        return persons;

    }
    public static PersonList operator -(PersonList persons, Person p)
    {
        persons.remove(p);
        return persons;
    }

    public void Save()
    {

        PersonDB.SavePersons(this);
    }
}

现在我要从Form2上传数据:

private PersonList persons = new PersonList();

             private void button2_Click(object sender, EventArgs e)
    {
        int i = 0;
        Form2 update = new Form2();
        Person updteDtl = (Person)lstPerson.SelectedItem;

        if (lstPerson.SelectedIndex != -1)
        {
            for (int j = 0; j < persons.Count; j++)
            {
                if (persons[j] == updteDtl)
                {
                    i = j;
                    break;
                }
            }
            update.UpdatingData = updteDtl;
            Person updatePerson = update.GetNewPerson();

            if (updatePerson != null)
            {
                persons[i] = updatePerson;
            }
        }
        else
        {
            MessageBox.Show("Select the person to update.");
        }

    }

在Form2上我试图上传数据。

private Person person = null;

    public Person GetNewPerson()
    {
        this.ShowDialog();
        return person;
    }
         private Person updatingData = new Person();
    public Person UpdatingData
    {
        set { updatingData = value; }
    }
    private void Form2_Load(object sender, EventArgs e)
    {
         if (updatingData is Students)
        {
            Students std = (Students)updatingData;

            txtName.Text =  std.Firstname;
            txtLName.Text = std.Lastname;
            txtEmail.Text = std.Email;
            txtAssessGrade.Text = std.AssessmentGrade.ToString();
            txtAssigGrade.Text = std.AssignmentGrade.ToString();
        }
        else if (updatingData is Teacher)
        {
            rbTeacher.Checked = true;
            Teacher teacher = (Teacher)updatingData;

            txtName.Text = teacher.Firstname;
            txtLName.Text = teacher.Lastname;
            txtEmail.Text = teacher.Email;
            txtAssessGrade.Text = teacher.RoomNumber.ToString();

        }                     
    }

我想从Form2中检索数据,我在列表框中选择并更新该数据 但是,当我运行它并单击更新按钮时,它会显示错误无法将“System.String”类型的对象强制转换为“ProductMaint.Person”。请告诉我如何修复它?

2 个答案:

答案 0 :(得分:1)

我认为这一行是抛出异常的那一行:

Person updteDtl = (Person)lstPerson.SelectedItem;

换句话说,lstPerson中的所选项目是字符串,而不是Person,因此您无法将其投放到Person

不幸的是,尽管您向我们展示了数百行代码,但您还没有告诉我们lstPerson期望 lstPerson.SelectedItem的原因一个Person。 (你甚至没有告诉我们你在写什么样的应用程序。)

这就是问题的原因,而且与您发布的代码关系不大 - 所以请关注这一部分。您可以更改绑定到列表的方式,也可以根据SelectedItem中的任何字符串找出获取相关人员的方法。

答案 1 :(得分:0)

我的猜测是问题源于这一行:

Person updteDtl = (Person)lstPerson.SelectedItem;

显然这会返回一个String,而不是Person个对象。我真的不确定是否可以在下拉列表中存储一个对象,例如Person的实例(我假设这是lstPerson是什么?)。

解决方案可能是使用其中一个字段来标识每个人,并将其用作下拉列表中项目的值(例如,电子邮件可能对每个人都是唯一的?否则,添加一个用于此目的的新字段PersonId)。由于下拉列表中的每个项目都可以同时显示value字段和文本字符串,因此您可以使用ID作为值,并使用显示文本的名称(或任何您想要的名称)。

在事件处理程序中,您必须获取value,然后使用它来查找Person列表中的人员。然后,您可以使用表单中的其余数据更新该人员。

我希望我能正确理解你的问题,这很明显,可以提供一些帮助,祝你好运!