none
怎么样在DataGridView里绑定嵌套属性的值? RRS feed

  • 问题

  • 初学winform 现在用NHibernate从数据库里取出这样两个类的实列:

        public class Achievement
        {
            private long _id;
            private long _studentId;
            private float _makeup2;
            private float _makeup1;
            private float _score;
            private float _credit;
            private string _property;
            private string _className;
            private string _semester;
            private Student _student;
    
            public virtual long Id
            {
                get { return _id; }
                set { _id = value; }
            }
    
            public virtual int Version
            {
                get;
                set;
            }
    
            public virtual string Semester
            {
                get { return _semester; }
                set { _semester = value; }
            }
    
            public virtual string ClassName
            {
                get { return _className; }
                set { _className = value; }
            }
    
            public virtual string Property
            {
                get { return _property; }
                set { _property = value; }
            }
    
            public virtual float Credit
            {
                get { return _credit; }
                set { _credit = value; }
            }
    
            public virtual float Score
            {
                get { return _score; }
                set { _score = value; }
            }
    
            public virtual float Makeup1
            {
                get { return _makeup1; }
                set { _makeup1 = value; }
            }
    
            public virtual float Makeup2
            {
                get { return _makeup2; }
                set { _makeup2 = value; }
            }
    
            public virtual Student Student
            {
                get { return _student; }
                set { _student = value; }
            }
    
            public virtual void EditWith(Achievement achievement)
            {
                this.ClassName = achievement.ClassName;
                this.Credit = achievement.Credit;
                this.Makeup1 = achievement.Makeup1;
                this.Makeup2 = achievement.Makeup2;
                this.Property = achievement.Property;
                this.Score = achievement.Score;
                this.Semester = achievement.Semester;
            }
        }
    和:

        public class Student
        {
            private long _studentID;
            private string _name;
            private string _major;
            private string _class;
            private string _birthDay;
            private string _pin;
            private string _gender;
            private string _politicalStatue;
            private string _dormitory;
            private string _homeAddress;
            private string _postalCode;
            private string _phone;
            private string _email;
            private string _qq;
            private string _remark;
            private string _ethn;
            private Parent _father;
            private Parent _mother;
            private ISet _achievements = new HashedSet();
    
            public virtual long StudentId
            {
                get { return _studentID; }
                set { _studentID = value;}
            }
    
            public virtual int Version { get; set; }
    
            public virtual string Name
            {
                get { return _name; }
                set { _name = value;}
            }
    
            public virtual string Major
            {
                get { return _major; }
                set { _major = value;}
            }
    
            public virtual string Class
            {
                get { return _class; }
                set { _class = value;}
            }
    
            public virtual string BirthDay
            {
                get { return _birthDay; }
                set { _birthDay = value;}
            }
    
            public virtual string PIN
            {
                get { return _pin; }
                set { _pin = value;}
            }
    
            public virtual string Gender
            {
                get { return _gender; }
                set { _gender = value;}
            }
    
            public virtual string PoliticalStatue
            {
                get { return _politicalStatue; }
                set { _politicalStatue = value;}
            }
    
            public virtual string Dormitory
            {
                get { return _dormitory; }
                set { _dormitory = value;}
            }
    
            public virtual string HomeAddress
            {
                get { return _homeAddress; }
                set { _homeAddress = value; }
            }
    
            public virtual string PostalCode
            {
                get { return _postalCode; }
                set { _postalCode = value; }
            }
    
            public virtual string Phone
            {
                get { return _phone; }
                set { _phone = value; }
            }
    
            public virtual string Email
            {
                get { return _email; }
                set { _email = value; }
            }
    
            public virtual string QQ
            {
                get { return _qq; }
                set { _qq = value; }
            }
    
            public virtual string Remark
            {
                get { return _remark; }
                set { _remark = value; }
            }
    
            public virtual string Ethn
            {
                get { return _ethn; }
                set { _ethn = value; }
            }
    
            public virtual Parent Father
            {
                get { return _father; }
                set { _father = value;}
            }
    
            public virtual Parent Mother
            {
                get { return _mother; }
                set { _mother = value;}
            }
    
            public virtual ISet Achievements
            {
                get { return _achievements; }
                set { _achievements = value; }
            }
    
            public virtual void AddAchievement(Achievement achievement)
            { 
                achievement.Student = this;
                //_achievements.Add(achievement);
                this.Achievements.Add(achievement);
            }
    
            public virtual bool IsValid()
            {
                if (this._birthDay != string.Empty && this._class != string.Empty && this._dormitory != string.Empty && this._email != string.Empty && this._ethn != string.Empty && this._father != null && this._gender != string.Empty && this._homeAddress != string.Empty && this._major != string.Empty && this._mother != null && this._name != string.Empty && this._phone != string.Empty && this._pin != string.Empty && this._politicalStatue != string.Empty && this._postalCode != string.Empty && this._qq != string.Empty && this._studentID != 0)
                {
                    return true;
                }
                return false;
            }
    
            public virtual int GetTotalCredit()
            {
                if (Achievements == null)
                {
                    return 0;
                }
                else
                {
                    return (int)Achievements.Cast<Achievement>().Sum(x => x.Credit);
                }
            }
    
            public virtual void EidtWith(Student stu)
            {
                this.BirthDay = stu.BirthDay;
                this.Class = stu.Class;
                this.Dormitory = stu.Dormitory;
                this.Email = stu.Email;
                this.Ethn = stu.Ethn;
                this.Father.Name = stu.Father.Name;
                this.Father.Phone = stu.Father.Phone;
                this.Gender = stu.Gender;
                this.HomeAddress = stu.HomeAddress;
                this.Major = stu.Major;
                this.Mother.Name = stu.Mother.Name;
                this.Mother.Phone = stu.Mother.Phone;
                this.Name = stu.Name;
                this.Phone = stu.Phone;
                this.PIN = stu.PIN;
                this.PoliticalStatue = stu.PoliticalStatue;
                this.PostalCode = stu.PostalCode;
                this.QQ = stu.QQ;
                this.Remark = stu.Remark;
            }
        }
    现在想把一个IList<Achievement>绑定到DataGridView上但在Student属性上出了问题...
    如果我想同时显示Student属性的Name怎么办?
    是不是为了绑定要从新新建一个包括Student.Name的新对象啊?
    希望大家给个思路 谢谢!
    2009年12月23日 7:01

答案

  • 你好!
         可以尝试重写Student类的ToString()方法:
        public class Student
        {
            public override string ToString()
            {
                return _name;
            }

    你绑定的时候这样:
                List<Achievement> list=new List<Achievement>();
                Achievement a1 = new Achievement();
                Student s1 = new Student();
                s1.Name = "aaa";
                a1.Student = s1;
                Achievement a2 = new Achievement();
                Student s2 = new Student();
                s2.Name = "bbb";
                a2.Student = s2;
                list.Add(a1);
                list.Add(a2);
                this.dataGridView1.DataSource = list;
    周雪峰
    • 已标记为答案 YiChun Chen 2009年12月24日 8:34
    2009年12月23日 8:18
    版主